synchronous-promise
Advanced tools
Comparing version 2.0.11 to 2.0.12
@@ -85,3 +85,3 @@ (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
var ran = false; | ||
function runFinally(result) { | ||
function runFinally(result, err) { | ||
if (!ran) { | ||
@@ -92,3 +92,13 @@ ran = true; | ||
} | ||
return callback(result); | ||
var callbackResult = callback(result); | ||
if (looksLikeAPromise(callbackResult)) { | ||
return callbackResult.then(function() { | ||
if (err) { | ||
throw err; | ||
} | ||
return result; | ||
}); | ||
} else { | ||
return result; | ||
} | ||
} | ||
@@ -101,3 +111,3 @@ } | ||
.catch(function(err) { | ||
return runFinally(err); | ||
return runFinally(null, err); | ||
}); | ||
@@ -104,0 +114,0 @@ }, |
@@ -0,0 +0,0 @@ /// <reference path="index.d.ts" /> |
@@ -0,0 +0,0 @@ export interface SynchronousPromise<T> extends Promise<T> { |
16
index.js
@@ -82,3 +82,3 @@ /* jshint node: true */ | ||
var ran = false; | ||
function runFinally(result) { | ||
function runFinally(result, err) { | ||
if (!ran) { | ||
@@ -89,3 +89,13 @@ ran = true; | ||
} | ||
return callback(result); | ||
var callbackResult = callback(result); | ||
if (looksLikeAPromise(callbackResult)) { | ||
return callbackResult.then(function() { | ||
if (err) { | ||
throw err; | ||
} | ||
return result; | ||
}); | ||
} else { | ||
return result; | ||
} | ||
} | ||
@@ -98,3 +108,3 @@ } | ||
.catch(function(err) { | ||
return runFinally(err); | ||
return runFinally(null, err); | ||
}); | ||
@@ -101,0 +111,0 @@ }, |
@@ -688,3 +688,3 @@ /* | ||
it("should resolve with all values from given promise or none promise variable args", function () { | ||
const all = SynchronousPromise.all([ "123", createResolved("abc") ]); | ||
const all = SynchronousPromise.all(["123", createResolved("abc")]); | ||
let captured = null; | ||
@@ -704,3 +704,3 @@ | ||
p2 = createResolved("123"), | ||
all = SynchronousPromise.all([ p1, p2 ]); | ||
all = SynchronousPromise.all([p1, p2]); | ||
let captured = null; | ||
@@ -741,3 +741,3 @@ | ||
SynchronousPromise.all([ p1, p2 ]).then(function (data) { | ||
SynchronousPromise.all([p1, p2]).then(function (data) { | ||
captured = data; | ||
@@ -749,3 +749,3 @@ }); | ||
expect(captured).to.deep.equal([ "b", "a" ]); | ||
expect(captured).to.deep.equal(["b", "a"]); | ||
}); | ||
@@ -825,3 +825,3 @@ | ||
}), | ||
expected = { key: "value" }; | ||
expected = {key: "value"}; | ||
// Act | ||
@@ -871,3 +871,3 @@ debugger; | ||
}), | ||
expected = { key: "value" }; | ||
expected = {key: "value"}; | ||
// Act | ||
@@ -951,3 +951,3 @@ sut.reject(expected); | ||
// Assert | ||
expect(captured).to.equal("second value"); | ||
expect(captured).to.equal("first value"); | ||
}); | ||
@@ -966,3 +966,3 @@ | ||
// Assert | ||
expect(captured).to.equal("second value"); | ||
expect(captured).to.equal("first value"); | ||
}); | ||
@@ -1014,2 +1014,3 @@ | ||
let captured = null; | ||
let capturedErr = null; | ||
// Act | ||
@@ -1021,5 +1022,8 @@ SynchronousPromise.reject("le error") | ||
captured = data; | ||
}).catch(function(err) { | ||
capturedErr = err; | ||
}); | ||
// Assert | ||
expect(captured).to.equal("le data"); | ||
expect(captured).to.be.null; | ||
expect(capturedErr).to.equal("le error"); | ||
}); | ||
@@ -1069,3 +1073,3 @@ | ||
expect(events).to.eql( | ||
[ "initial", "then", "finally" ] | ||
["initial", "then", "finally"] | ||
); | ||
@@ -1092,3 +1096,3 @@ }); | ||
expect(events).to.eql( | ||
[ "initial", "then1", "then2 received: then1", "then2", "finally" ] | ||
["initial", "then1", "then2 received: then1", "then2", "finally"] | ||
); | ||
@@ -1127,7 +1131,7 @@ }); | ||
}); | ||
expect(events).to.eql([ "result: init" ]); | ||
expect(events).to.eql(["result: init"]); | ||
// Act | ||
promise.resume(); | ||
// Assert | ||
expect(events).to.eql([ "result: init", "resumed", "finally" ]); | ||
expect(events).to.eql(["result: init", "resumed", "finally"]); | ||
}); | ||
@@ -1154,2 +1158,12 @@ | ||
it(`should pass last result onto next .then when finally has an empty handler fn`, async () => { | ||
// Arrange | ||
// Act | ||
const result = await SynchronousPromise.resolve("expected") | ||
.finally(() => {}); | ||
// Assert | ||
expect(result) | ||
.to.equal("expected"); | ||
}); | ||
describe(`imported specs from blalasaadri`, () => { | ||
@@ -1174,3 +1188,3 @@ // these relate to https://github.com/fluffynuts/synchronous-promise/issues/15 | ||
expect(events) | ||
.to.eql([ "init", "result: resolve", "then" ]) | ||
.to.eql(["init", "result: resolve", "then"]) | ||
}); | ||
@@ -1195,3 +1209,3 @@ | ||
expect(events) | ||
.to.eql([ "init", "error: reject" ]) | ||
.to.eql(["init", "error: reject"]) | ||
}); | ||
@@ -1215,3 +1229,3 @@ | ||
expect(events) | ||
.to.eql([ "result: init", "then", "finally" ]) | ||
.to.eql(["result: init", "then", "finally"]) | ||
}); | ||
@@ -1238,3 +1252,3 @@ | ||
expect(events) | ||
.to.eql([ "error: init", "finally" ]) | ||
.to.eql(["error: init", "finally"]) | ||
}) | ||
@@ -1271,3 +1285,3 @@ }); | ||
expect(events).to.eql([ "result: resolve", "then" ]) | ||
expect(events).to.eql(["result: resolve", "then"]) | ||
}) | ||
@@ -1292,3 +1306,3 @@ }); | ||
expect(events) | ||
.to.eql([ "error: reject" ]) | ||
.to.eql(["error: reject"]) | ||
}); | ||
@@ -1333,5 +1347,5 @@ | ||
expect(events) | ||
.not.to.eql([ "finally", "result: undefined", "then" ]); | ||
.not.to.eql(["finally", "result: undefined", "then"]); | ||
expect(events) | ||
.to.eql([ "result: resolve", "then", "finally" ]) | ||
.to.eql(["result: resolve", "then", "finally"]) | ||
}) | ||
@@ -1377,5 +1391,5 @@ }); | ||
expect(events) | ||
.not.to.eql([ "finally", "result: undefined" ]); | ||
.not.to.eql(["finally", "result: undefined"]); | ||
expect(events) | ||
.to.eql([ "error: reject", "finally" ]) | ||
.to.eql(["error: reject", "finally"]) | ||
}) | ||
@@ -1400,3 +1414,3 @@ }) | ||
expect(events) | ||
.to.eql([ "result: init" ]) | ||
.to.eql(["result: init"]) | ||
}); | ||
@@ -1418,3 +1432,3 @@ | ||
expect(events) | ||
.to.eql([ "result: init", "resumed" ]) | ||
.to.eql(["result: init", "resumed"]) | ||
}) | ||
@@ -1433,3 +1447,3 @@ }); | ||
.pause() | ||
.catch(({ message }) => { | ||
.catch(({message}) => { | ||
events.push(`catch: ${message}`) | ||
@@ -1439,3 +1453,3 @@ }); | ||
expect(events) | ||
.to.eql([ "result: init" ]) | ||
.to.eql(["result: init"]) | ||
}); | ||
@@ -1452,3 +1466,3 @@ | ||
.pause() | ||
.catch(({ message }) => { | ||
.catch(({message}) => { | ||
events.push(`catch: ${message}`) | ||
@@ -1459,3 +1473,3 @@ }); | ||
expect(events) | ||
.to.eql([ "result: init", "catch: resumed" ]) | ||
.to.eql(["result: init", "catch: resumed"]) | ||
}) | ||
@@ -1483,3 +1497,3 @@ }); | ||
expect(events) | ||
.to.eql([ "result: init" ]) | ||
.to.eql(["result: init"]) | ||
}); | ||
@@ -1504,5 +1518,5 @@ | ||
expect(events) | ||
.not.to.eql([ "result: init", "finally", "resumed" ]); | ||
.not.to.eql(["result: init", "finally", "resumed"]); | ||
expect(events) | ||
.to.eql([ "result: init", "resumed", "finally" ]) | ||
.to.eql(["result: init", "resumed", "finally"]) | ||
}) | ||
@@ -1521,3 +1535,3 @@ }); | ||
.pause() | ||
.catch(({ message }) => { | ||
.catch(({message}) => { | ||
events.push(`catch: ${message}`) | ||
@@ -1532,3 +1546,3 @@ }) | ||
expect(events) | ||
.to.eql([ "result: init" ]) | ||
.to.eql(["result: init"]) | ||
}); | ||
@@ -1545,3 +1559,3 @@ | ||
.pause() | ||
.catch(({ message }) => { | ||
.catch(({message}) => { | ||
events.push(`catch: ${message}`) | ||
@@ -1555,5 +1569,5 @@ }) | ||
expect(events) | ||
.not.to.eql([ "result: init", "finally", "catch: resumed" ]); | ||
.not.to.eql(["result: init", "finally", "catch: resumed"]); | ||
expect(events) | ||
.to.eql([ "result: init", "catch: resumed", "finally" ]) | ||
.to.eql(["result: init", "catch: resumed", "finally"]) | ||
}) | ||
@@ -1560,0 +1574,0 @@ }) |
{ | ||
"name": "synchronous-promise", | ||
"version": "2.0.11", | ||
"version": "2.0.12", | ||
"description": "Synchronous Promise-like prototype to use in testing where you would have used an ES6 Promise", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,0 +0,0 @@ # synchronous-promise |
@@ -0,0 +0,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12
2285
90399