brianmhunt-mutex-promise
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -165,7 +165,7 @@ "use strict"; | ||
return this.then(function (value) { | ||
return Promise.resolve(cb()).then(function () { | ||
return MutexPromise.resolve(cb()).then(function () { | ||
return value; | ||
}); | ||
}, function (reason) { | ||
return Promise.resolve(cb()).then(function () { | ||
return MutexPromise.resolve(cb()).then(function () { | ||
throw reason; | ||
@@ -396,3 +396,3 @@ }); | ||
MutexPromise.race = function race(iter) { | ||
return new Promise(function (res, rej) { | ||
return new MutexPromise(function (res, rej) { | ||
var weCatchFor = this.weCatchFor; | ||
@@ -410,26 +410,39 @@ iter.forEach(function (p) { | ||
var arr = []; | ||
var promises = []; | ||
var seen = 0; | ||
return new Promise(function (res, rej) { | ||
var weCatchFor = this.weCatchFor; | ||
iter.forEach(function (p) { | ||
var all = new MutexPromise(function (res, rej) { | ||
iter.forEach(function (valueOrPromise) { | ||
var p = MutexPromise.resolve(valueOrPromise); | ||
var idx = arr.length; | ||
arr.push(undefined); | ||
p.then(function (value) { | ||
if (valueOrPromise instanceof MutexPromise) { | ||
promises.push(p); | ||
} | ||
MutexPromise.resolve(p).then(function (value) { | ||
arr[idx] = value; | ||
if (seen++ === arr.length) { | ||
if (++seen === arr.length) { | ||
res(arr); | ||
} | ||
}); | ||
p.catch(rej); | ||
weCatchFor.push(p); | ||
}, rej); | ||
}); | ||
}); | ||
promises.forEach(function (p) { | ||
all.weCatchFor.push(p); | ||
}); | ||
return all; | ||
}; | ||
MutexPromise.resolve = function (value) { | ||
return new MutexPromise(function (res) { | ||
return res(value); | ||
MutexPromise.resolve = function (valueOrThenableOrPromise) { | ||
var rp = new MutexPromise(function (res) { | ||
return res(valueOrThenableOrPromise); | ||
}); | ||
if (valueOrThenableOrPromise instanceof MutexPromise) { | ||
rp.weCatchFor.push(valueOrThenableOrPromise); | ||
} | ||
return rp; | ||
}; | ||
MutexPromise.reject = function (reason) { | ||
@@ -436,0 +449,0 @@ return new MutexPromise(function (_, rej) { |
{ | ||
"name": "brianmhunt-mutex-promise", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Promises with uncaught handling and events", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -141,4 +141,4 @@ "use strict" | ||
return this.then( | ||
(value) => Promise.resolve(cb()).then(() => value), | ||
(reason) => Promise.resolve(cb()).then(function () { throw reason }) | ||
(value) => MutexPromise.resolve(cb()).then(() => value), | ||
(reason) => MutexPromise.resolve(cb()).then(function () { throw reason }) | ||
) | ||
@@ -332,3 +332,3 @@ } | ||
MutexPromise.race = function race(iter) { | ||
return new Promise(function (res, rej) { | ||
return new MutexPromise(function (res, rej) { | ||
var weCatchFor = this.weCatchFor | ||
@@ -346,20 +346,36 @@ iter.forEach(function (p) { | ||
var arr = [] | ||
var promises = [] | ||
var seen = 0 | ||
return new Promise(function (res, rej) { | ||
var weCatchFor = this.weCatchFor | ||
iter.forEach(function (p) { | ||
var all = new MutexPromise(function (res, rej) { | ||
iter.forEach(function (valueOrPromise) { | ||
var p = MutexPromise.resolve(valueOrPromise) | ||
var idx = arr.length | ||
arr.push(undefined) | ||
p.then(function (value) { | ||
arr[idx] = value | ||
if (seen++ === arr.length) { res(arr) } | ||
}) | ||
p.catch(rej) | ||
weCatchFor.push(p) | ||
if (valueOrPromise instanceof MutexPromise) { | ||
promises.push(p) | ||
} | ||
MutexPromise.resolve(p) | ||
.then(function (value) { | ||
arr[idx] = value | ||
if (++seen === arr.length) { res(arr) } | ||
}, rej) | ||
}) | ||
}) | ||
promises.forEach(function (p) { | ||
all.weCatchFor.push(p) | ||
}) | ||
return all | ||
} | ||
MutexPromise.resolve = (value) => new MutexPromise((res) => res(value)) | ||
MutexPromise.resolve = function (valueOrThenableOrPromise) { | ||
var rp = new MutexPromise((res) => res(valueOrThenableOrPromise)) | ||
if (valueOrThenableOrPromise instanceof MutexPromise) { | ||
rp.weCatchFor.push(valueOrThenableOrPromise) | ||
} | ||
return rp | ||
} | ||
MutexPromise.reject = (reason) => new MutexPromise((_, rej) => rej(reason)) | ||
@@ -366,0 +382,0 @@ |
@@ -8,2 +8,6 @@ // | ||
// We do not ever want to refer to the global promise. | ||
global.Promise = null | ||
const MP = require('../src/MutexPromise') | ||
@@ -34,2 +38,3 @@ | ||
// Run the Promises A+ suite | ||
describe("Promises A+", function () { | ||
@@ -39,2 +44,4 @@ require('promises-aplus-tests').mocha(APLUS_ADAPTER) | ||
// Out MutexPromise-specific tests | ||
describe("MutexPromise", function () { | ||
@@ -65,13 +72,38 @@ beforeEach(function () { MP.setMutex(this.currentTest.title) }) | ||
it("instances have .finally", function () { | ||
var val = {x: '123'} | ||
var called = false | ||
return MP.reject(val) | ||
.finally(() => called = true) | ||
.then(function () { throw Error("Do not call") }, () => assert.ok(called)) | ||
}) | ||
it("throws if no function is given", function() { | ||
it("throws if no function is given to constructor", function() { | ||
assert.throws(() => new MP(), /is not a function/) | ||
}) | ||
describe(".finally", function () { | ||
it("taps resolutions", function () { | ||
var val = { x: '123' } | ||
var called = false | ||
return MP.resolve(val) | ||
.finally(() => called = true) | ||
.then( | ||
function (reason) { | ||
assert.ok(called) | ||
assert.strictEqual(reason, val) | ||
}, | ||
function () { throw Error("Do not call") } | ||
) | ||
}) | ||
it("taps rejections", function () { | ||
var val = { x: '123' } | ||
var called = false | ||
return MP.reject(val) | ||
.finally(() => called = true) | ||
.then( | ||
function () { throw Error("Do not call") }, | ||
function (reason) { | ||
assert.ok(called) | ||
assert.strictEqual(reason, val) | ||
} | ||
) | ||
}) | ||
}) | ||
describe('events', function () { | ||
@@ -197,2 +229,30 @@ it("triggers 'new'", function() { | ||
}) | ||
describe("Promise.all", function () { | ||
it("Returns the result of all promises", function () { | ||
return MP.all([ | ||
MP.resolve('ab'), | ||
MP.resolve('a').then(() => 'b'), | ||
'step', | ||
null | ||
]).then(function (results) { | ||
assert.equal(results[0], 'ab') | ||
assert.equal(results[1], 'b') | ||
assert.equal(results[2], 'step') | ||
assert.equal(results[3], null) | ||
}) | ||
}) | ||
it.skip("Marks its argument-promises as caught", function () { | ||
var p0 = MP.resolve('ab') | ||
return MP.all([p0]) | ||
.then(() => assert.notOk(p0.isCaught)) | ||
.catch(noop) // this occurs before weCatchFor.push; FIXME | ||
.then(() => assert.ok(p0.isCaught)) | ||
}) | ||
}) | ||
// describe("Promise.race", function () { | ||
// | ||
// }) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39115
967