synchronous-promise
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -5,2 +5,4 @@ export interface SynchronousPromise<T> extends Promise<T> { | ||
} | ||
export type ValueOrPromiseOfValue<T> = T | PromiseLike<T> | ||
export interface SynchronousPromiseConstructor { | ||
@@ -26,3 +28,4 @@ /** | ||
*/ | ||
all<T>(values: IterableShim<T | PromiseLike<T>>): SynchronousPromise<T[]>; | ||
all<T>(v1: ValueOrPromiseOfValue<T>[]): SynchronousPromise<T[]>; | ||
all<TAll>(...values: ValueOrPromiseOfValue<TAll>[]): SynchronousPromise<TAll[]>; | ||
@@ -29,0 +32,0 @@ /** |
19
index.js
'use strict'; | ||
function toArray(args) { | ||
function argumentsToArray(args) { | ||
return Array.prototype.slice.apply(args); | ||
} | ||
function SynchronousPromise(ctorFunction) { | ||
@@ -37,7 +38,7 @@ this.status = 'pending'; | ||
var doCatch = function (args) { | ||
self._catchData = toArray(args); | ||
self._catchData = argumentsToArray(args); | ||
self._applyCatch(); | ||
}; | ||
ctorFunction(function () { | ||
self._data = toArray(arguments); | ||
self._data = argumentsToArray(arguments); | ||
var doResolve = function () { | ||
@@ -49,3 +50,3 @@ self.status = 'resolved'; | ||
self._data[0].then(function () { | ||
self._data = toArray(arguments); | ||
self._data = argumentsToArray(arguments); | ||
doResolve(); | ||
@@ -123,3 +124,3 @@ }).catch(function () { | ||
return new SynchronousPromise(function (resolve) { | ||
resolve.apply(null, toArray(args)); | ||
resolve.apply(null, argumentsToArray(args)); | ||
}); | ||
@@ -130,7 +131,11 @@ }; | ||
return new SynchronousPromise(function (resolve, reject) { | ||
reject.apply(null, toArray(args)); | ||
reject.apply(null, argumentsToArray(args)); | ||
}); | ||
}; | ||
SynchronousPromise.all = function () { | ||
var args = toArray(arguments); | ||
//var args = argumentsToArray(arguments); | ||
var args = argumentsToArray(arguments); | ||
if (Array.isArray(args[0])) { | ||
args = args[0]; | ||
} | ||
return new SynchronousPromise(function (resolve, reject) { | ||
@@ -137,0 +142,0 @@ var |
@@ -8,4 +8,5 @@ /* | ||
expect = require('chai').expect, | ||
SynchronousPromise = require('./index').SynchronousPromise; | ||
console.log('-- test run at: ', new Date()); | ||
sut = require('./index'), | ||
SynchronousPromise = sut.SynchronousPromise, | ||
_argumentsToArray = sut._argumentsToArray; | ||
describe('synchronous-promise', function () { | ||
@@ -26,10 +27,6 @@ it('should be constructable', function () { | ||
function createResolved(data) { | ||
return create(function (resolve, reject) { | ||
resolve(data); | ||
}); | ||
return SynchronousPromise.resolve(data); | ||
} | ||
function createRejected(data) { | ||
return create(function (resolve, reject) { | ||
reject(data); | ||
}); | ||
return SynchronousPromise.reject(data); | ||
} | ||
@@ -351,2 +348,18 @@ describe('then', function () { | ||
}); | ||
it('should resume a promise which was started rejected as rejected', function() { | ||
var | ||
calls = 0, | ||
captured = null, | ||
expected = 'it\'s the end of the world!', | ||
promise = SynchronousPromise.reject(expected).pause().then(function() { | ||
calls++; | ||
}).catch(function(e) { | ||
captured = e; | ||
}); | ||
expect(calls).to.equal(0); | ||
expect(captured).to.be.null; | ||
promise.resume(); | ||
expect(calls).to.equal(0); | ||
expect(captured).to.equal(expected); | ||
}) | ||
}) | ||
@@ -389,3 +402,3 @@ describe('static resolve', function () { | ||
}) | ||
it('should resolve with all values from given resolved promised', function () { | ||
it('should resolve with all values from given resolved promises as variable args', function () { | ||
var | ||
@@ -396,2 +409,3 @@ p1 = createResolved('abc'), | ||
captured = null; | ||
all.then(function (data) { | ||
@@ -405,2 +419,17 @@ captured = data; | ||
}); | ||
it('should resolve with all values from given resolved promises as an array', function () { | ||
var | ||
p1 = createResolved('abc'), | ||
p2 = createResolved('123'), | ||
all = SynchronousPromise.all([p1, p2]), | ||
captured = null; | ||
all.then(function (data) { | ||
captured = data; | ||
}); | ||
expect(captured).to.have.length(2); | ||
expect(captured).to.contain('abc'); | ||
expect(captured).to.contain('123'); | ||
}); | ||
it('should reject if any promise rejects', function () { | ||
@@ -407,0 +436,0 @@ var |
{ | ||
"name": "synchronous-promise", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Synchronous Promise-like prototype to use in testing where you would have used an ES6 Promise", | ||
@@ -11,8 +11,10 @@ "main": "index.js", | ||
"scripts": { | ||
"autotest": "mocha --watch index.spec.js --reporter mocha-yar", | ||
"autolint": "nodemon -x \"npm run lint\"", | ||
"preautotest-once": "node -e \"console.log('Tests started: ', new Date());\"", | ||
"autotest-once": "mocha *.spec.js --reporter mocha-yar", | ||
"autotest": "nodemon -q -x \"run-s -s autotest-once\"", | ||
"autolint": "nodemon -q -x \"run-s -s run lint\"", | ||
"lint": "jslint index.js", | ||
"pretest": "run-s lint", | ||
"test": "mocha index.spec.js", | ||
"prepublish": "run-s test" | ||
"prepublish": "run-s -s test" | ||
}, | ||
@@ -26,3 +28,3 @@ "author": "Davyd McColl <davydm@gmail.com> (https://github.com/fluffynuts)", | ||
"mocha-yar": "^1.0.10", | ||
"nodemon": "^1.10.0", | ||
"nodemon": "^1.10.2", | ||
"npm-run-all": "^2.3.0", | ||
@@ -29,0 +31,0 @@ "run-sequence": "^1.2.2" |
@@ -0,0 +0,0 @@ # synchronous-promise |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
30402
651