From 84b87026e1f602ba11b6924021fb1082c2c00689 Mon Sep 17 00:00:00 2001 From: bnaya Date: Wed, 14 Jun 2017 23:51:16 +0300 Subject: [PATCH 1/3] Make methods return bluebird promise instead native promise. --- promiseFns/any.js | 5 ++++- promiseFns/call.js | 4 +++- promiseFns/delay.js | 4 +++- promiseFns/each.js | 26 ++++++++++++++------------ promiseFns/filter.js | 5 ++++- promiseFns/get.js | 4 +++- promiseFns/map.js | 4 +++- promiseFns/mapSeries.js | 4 +++- promiseFns/props.js | 4 +++- promiseFns/reduce.js | 4 +++- promiseFns/some.js | 5 ++++- promiseFns/timeout.js | 4 +++- 12 files changed, 50 insertions(+), 23 deletions(-) diff --git a/promiseFns/any.js b/promiseFns/any.js index 11e7922..5eeef5b 100644 --- a/promiseFns/any.js +++ b/promiseFns/any.js @@ -1,5 +1,8 @@ module.exports = (Bluebird) => { - Bluebird.any = (prom, n) => Bluebird.resolve(prom).any(); + 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")} diff --git a/promiseFns/call.js b/promiseFns/call.js index abe5994..8462cda 100644 --- a/promiseFns/call.js +++ b/promiseFns/call.js @@ -10,5 +10,7 @@ module.exports = (Bluebird) => { })()); }; - Bluebird.call = (o, ...args) => Bluebird.resolve(o).call(...args); + Bluebird.call = (o, ...args) => Bluebird.resolve((async () => + Bluebird.resolve(o).call(...args) + )()); }; diff --git a/promiseFns/delay.js b/promiseFns/delay.js index 61978ed..0779c71 100644 --- a/promiseFns/delay.js +++ b/promiseFns/delay.js @@ -2,5 +2,7 @@ module.exports = (Bluebird) => { Bluebird.prototype.delay = function delay(ms) { return this.then(obj => new Bluebird((onFulfilled) => setTimeout(() => onFulfilled(obj), ms))); } - Bluebird.delay = (ms, o) => Bluebird.resolve(o).delay(ms); + Bluebird.delay = (ms, o) => Bluebird.resolve((async () => + Bluebird.resolve(o).delay(ms) + )()); } diff --git a/promiseFns/each.js b/promiseFns/each.js index 32257d4..53678b1 100644 --- a/promiseFns/each.js +++ b/promiseFns/each.js @@ -1,15 +1,17 @@ module.exports = (Bluebird) => { - Bluebird.prototype.each = function each(iterator) { - return Bluebird.resolve((async () => { - const promises = await Promise.all(await this); - const length = promises.length; - - for(let index = 0; index <= promises.length-1; index++){ - const value = await iterator(await promises[index], index, length); - }; - return promises; - })()); - }; + Bluebird.prototype.each = function each(iterator) { + return Bluebird.resolve((async () => { + const promises = await Promise.all(await this); + const length = promises.length; - Bluebird.each = (promise, iterator) => Bluebird.resolve(promise).each(iterator); + for(let index = 0; index <= promises.length-1; index++){ + const value = await iterator(await promises[index], index, length); + }; + return promises; + })()); + }; + + Bluebird.each = (promise, iterator) => Bluebird.resolve((async () => + Bluebird.resolve(promise).each(iterator) + )()); }; \ No newline at end of file diff --git a/promiseFns/filter.js b/promiseFns/filter.js index 14492a7..22b5b7a 100644 --- a/promiseFns/filter.js +++ b/promiseFns/filter.js @@ -1,6 +1,9 @@ const util = require("./util"); module.exports = (Bluebird) => { - Bluebird.filter = (x, predicate, opts) => Bluebird.resolve(x).filter(predicate, opts); + 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}); diff --git a/promiseFns/get.js b/promiseFns/get.js index e0a6ed0..02e65c9 100644 --- a/promiseFns/get.js +++ b/promiseFns/get.js @@ -16,5 +16,7 @@ module.exports = (Bluebird) => { })()); }; - Bluebird.get = (promise, prop) => Bluebird.resolve(promise).get(prop); + Bluebird.get = (promise, prop) => Bluebird.resolve((async () => + Bluebird.resolve(promise).get(prop) + )()); }; diff --git a/promiseFns/map.js b/promiseFns/map.js index d2121d8..b9a44e6 100644 --- a/promiseFns/map.js +++ b/promiseFns/map.js @@ -1,6 +1,8 @@ const util = require("./util"); module.exports = (Bluebird) => { - Bluebird.map = (x, mapper, opts) => Bluebird.resolve(x).map(mapper, opts); + 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); diff --git a/promiseFns/mapSeries.js b/promiseFns/mapSeries.js index 842df16..7dee902 100644 --- a/promiseFns/mapSeries.js +++ b/promiseFns/mapSeries.js @@ -14,5 +14,7 @@ module.exports = (Bluebird) => { })()); }; - Bluebird.mapSeries = (promise, iterator) => Bluebird.resolve(promise).mapSeries(iterator); + Bluebird.mapSeries = (promise, iterator) => Bluebird.resolve((async () => + Bluebird.resolve(promise).mapSeries(iterator) + )()); }; \ No newline at end of file diff --git a/promiseFns/props.js b/promiseFns/props.js index 1dcc7d3..7ea5773 100644 --- a/promiseFns/props.js +++ b/promiseFns/props.js @@ -11,5 +11,7 @@ module.exports = (Bluebird) => { return ret; })()); }; - Bluebird.props = o => Bluebird.resolve(o).props(); + Bluebird.props = o => Bluebird.resolve((async () => + Bluebird.resolve(o).props() + )()); }; \ No newline at end of file diff --git a/promiseFns/reduce.js b/promiseFns/reduce.js index 88c84c6..16ce2f4 100644 --- a/promiseFns/reduce.js +++ b/promiseFns/reduce.js @@ -22,5 +22,7 @@ module.exports = (Bluebird) => { })()); }; - Bluebird.reduce = (promise, reducer, initialValue) => Bluebird.resolve(promise).reduce(reducer, initialValue); + Bluebird.reduce = (promise, reducer, initialValue) => Bluebird.resolve((async () => + Bluebird.resolve(promise).reduce(reducer, initialValue) + )()); }; \ No newline at end of file diff --git a/promiseFns/some.js b/promiseFns/some.js index e8305b2..38ac8bf 100644 --- a/promiseFns/some.js +++ b/promiseFns/some.js @@ -1,5 +1,8 @@ module.exports = (Bluebird) => { - Bluebird.some = (prom, n) => Bluebird.resolve(prom).some(n); + Bluebird.some = (prom, n) => Bluebird.resolve((async () => + Bluebird.resolve(prom).some(n) + )()); + Bluebird.prototype.some = function(n) { return Bluebird.resolve((async () => { let count = 0; diff --git a/promiseFns/timeout.js b/promiseFns/timeout.js index 176d84a..d599afa 100644 --- a/promiseFns/timeout.js +++ b/promiseFns/timeout.js @@ -22,5 +22,7 @@ module.exports = (Bluebird) => { return winner; })()); } - Bluebird.timeout = (ms, rejectDesc, o) => Bluebird.resolve(o).delay(ms) + Bluebird.timeout = (ms, rejectDesc, o) => Bluebird.resolve((async () => + Bluebird.resolve(o).delay(ms) + )()); } From a327e546cd261981e356db6ba8c1d9a03328062d Mon Sep 17 00:00:00 2001 From: bnaya Date: Thu, 15 Jun 2017 23:26:54 +0300 Subject: [PATCH 2/3] Make methods return bluebird promise instead native promise. --- promiseFns/any.js | 5 ++++- promiseFns/filter.js | 5 ++++- promiseFns/map.js | 4 +++- tests/any.js | 10 +++++++++- tests/filter.js | 5 +++++ tests/map.js | 5 +++++ 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/promiseFns/any.js b/promiseFns/any.js index 11e7922..5eeef5b 100644 --- a/promiseFns/any.js +++ b/promiseFns/any.js @@ -1,5 +1,8 @@ module.exports = (Bluebird) => { - Bluebird.any = (prom, n) => Bluebird.resolve(prom).any(); + 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")} diff --git a/promiseFns/filter.js b/promiseFns/filter.js index 14492a7..22b5b7a 100644 --- a/promiseFns/filter.js +++ b/promiseFns/filter.js @@ -1,6 +1,9 @@ const util = require("./util"); module.exports = (Bluebird) => { - Bluebird.filter = (x, predicate, opts) => Bluebird.resolve(x).filter(predicate, opts); + 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}); diff --git a/promiseFns/map.js b/promiseFns/map.js index d2121d8..b9a44e6 100644 --- a/promiseFns/map.js +++ b/promiseFns/map.js @@ -1,6 +1,8 @@ const util = require("./util"); module.exports = (Bluebird) => { - Bluebird.map = (x, mapper, opts) => Bluebird.resolve(x).map(mapper, opts); + 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); diff --git a/tests/any.js b/tests/any.js index 25931e8..a15d074 100644 --- a/tests/any.js +++ b/tests/any.js @@ -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([ @@ -56,4 +55,13 @@ 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) + }) }); \ No newline at end of file diff --git a/tests/filter.js b/tests/filter.js index fc651e2..bbbc589 100644 --- a/tests/filter.js +++ b/tests/filter.js @@ -29,6 +29,11 @@ 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) + }) }); const specify = it; diff --git a/tests/map.js b/tests/map.js index 25ac5c3..9e2f3de 100644 --- a/tests/map.js +++ b/tests/map.js @@ -29,6 +29,11 @@ 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) + }) }); const specify = it; From 6cc65ca9d8ecc71bdbac1400b468632701d501f7 Mon Sep 17 00:00:00 2001 From: bnaya Date: Sat, 1 Jul 2017 23:50:00 +0300 Subject: [PATCH 3/3] Add tests to validate non-bluebird instance --- tests/any.js | 9 +++++++++ tests/filter.js | 5 +++++ tests/map.js | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/tests/any.js b/tests/any.js index a15d074..900cc58 100644 --- a/tests/any.js +++ b/tests/any.js @@ -64,4 +64,13 @@ describe("any", () => { ]); 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) + }) }); \ No newline at end of file diff --git a/tests/filter.js b/tests/filter.js index bbbc589..9233c0e 100644 --- a/tests/filter.js +++ b/tests/filter.js @@ -34,6 +34,11 @@ describe("filter", () => { 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; diff --git a/tests/map.js b/tests/map.js index 9e2f3de..aeec35a 100644 --- a/tests/map.js +++ b/tests/map.js @@ -34,6 +34,11 @@ describe("map", () => { 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;