Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion promiseFns/any.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = (Bluebird) => {
Bluebird.any = (prom, n) => Bluebird.resolve(prom).any();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wasn't..

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But Bluebird.resolve already returns a bluebird instance, so why would wrapping help?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you resolve before you do the any, you need to resolve the any values.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But .any() already returns a bluebird promise.

Bluebird.any = (prom, n) => Bluebird.resolve((async () =>
Bluebird.resolve(prom).any()
)());

Bluebird.prototype.any = async function() {
// const items = await this;
// if(items.length === 0) { throw new TypeError("0 promises passed to any")}
Expand Down
5 changes: 4 additions & 1 deletion promiseFns/filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const util = require("./util");
module.exports = (Bluebird) => {
Bluebird.filter = (x, predicate, opts) => Bluebird.resolve(x).filter(predicate, opts);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wasn't..

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But Bluebird.resolve already returns a bluebird instance, so why would wrapping help?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you resolve before you do the filter, you need to resolve the filter values.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But p.filter(predicate) already returns a bluebird promise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not, filter, each and map didn't return bluebird promise, I have tested it.
I can move bluebird resolve wrapper to the prototype functions if it seems cleaner to you.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test so we can see?

Bluebird.filter = (x, predicate, opts) => Bluebird.resolve((async () =>
Bluebird.resolve(x).filter(predicate, opts)
)());

Bluebird.prototype.filter = async function(predicate, {concurrency} = {}) {
const values = await this.all();
const predicateResults = await this.map(predicate, {concurrency});
Expand Down
4 changes: 3 additions & 1 deletion promiseFns/map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const util = require("./util");
module.exports = (Bluebird) => {
Bluebird.map = (x, mapper, opts) => Bluebird.resolve(x).map(mapper, opts);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unnecessary.

Bluebird.map = (x, mapper, opts) => Bluebird.resolve((async () =>
Bluebird.resolve(x).map(mapper, opts)
)());

Bluebird.prototype.map = async function(mapper, {concurrency} = {}) {
const values = await Bluebird.all(await this);
Expand Down
19 changes: 18 additions & 1 deletion tests/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe("any", () => {
]);
assert.equal(v, 2);
});


it("resolves with two rejections and a resolve ordered", async () => {
const v = await Promise.any([
Expand All @@ -56,4 +55,22 @@ describe("any", () => {
]);
assert.equal(v, 2);
});

it('any should return bluebird promise', () => {
const v = Promise.any([
Promise.reject(0),
Promise.reject(1),
Promise.resolve(2)
]);
assert.ok(v instanceof Promise)
})

it('prototype.any should not return bluebird promise', () => {
const v = Promise.prototype.any([
Promise.reject(0),
Promise.reject(1),
Promise.resolve(2)
]);
assert.equal(v instanceof Promise, false)
})
});
10 changes: 10 additions & 0 deletions tests/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ describe("filter", () => {
});
assert.deepEqual(arr, [2]);
});

it('filter should return bluebird promise', () => {
const arr = Promise.filter([1,2,3], x => x)
assert.ok(arr instanceof Promise)
})

it('prototype.filter should not return bluebird promise', () => {
const arr = Promise.prototype.filter([1,2,3], x => x)
assert.equal(arr instanceof Promise, false)
})
});

const specify = it;
Expand Down
10 changes: 10 additions & 0 deletions tests/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ describe("map", () => {
});
assert.deepEqual(arr, [2,3,4]);
});

it('map should return bluebird promise', () => {
const arr = Promise.map([1,2,3], x => x)
assert.ok(arr instanceof Promise)
})

it('prototype.map should not return bluebird promise', () => {
const arr = Promise.prototype.map([1,2,3], x => x)
assert.equal(arr instanceof Promise, false)
})
});

const specify = it;
Expand Down