Socket
Socket
Sign inDemoInstall

synchronous-promise

Package Overview
Dependencies
0
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.9 to 2.0.10

15

dist/synchronous-promise.js

@@ -80,7 +80,11 @@ (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){

finally: function(callback) {
return (this._finally = SynchronousPromise.resolve()
._setParent(this)
.then(function() {
var ran = false;
function runFinally() {
if (!ran) {
ran = true;
return callback();
}));
}
}
return this.then(runFinally)
.catch(runFinally);
},

@@ -180,3 +184,3 @@ pause: function () {

_runResolutions: function () {
if (this._paused || !this._isResolved()) {
if (this._paused || !this._isResolved() || this._isPending()) {
return;

@@ -366,2 +370,3 @@ }

};
},{}]},{},[1]);

@@ -0,0 +0,0 @@ /// <reference path="index.d.ts" />

@@ -0,0 +0,0 @@ export interface SynchronousPromise<T> extends Promise<T> {

@@ -77,7 +77,11 @@ /* jshint node: true */

finally: function(callback) {
return (this._finally = SynchronousPromise.resolve()
._setParent(this)
.then(function() {
var ran = false;
function runFinally() {
if (!ran) {
ran = true;
return callback();
}));
}
}
return this.then(runFinally)
.catch(runFinally);
},

@@ -177,3 +181,3 @@ pause: function () {

_runResolutions: function () {
if (this._paused || !this._isResolved()) {
if (this._paused || !this._isResolved() || this._isPending()) {
return;

@@ -362,2 +366,2 @@ }

SynchronousPromise: SynchronousPromise
};
};

@@ -6,11 +6,11 @@ /*

"use strict";
var
expect = require("chai").expect,
const expect = require("chai").expect,
sut = require("./index"),
SynchronousPromise = sut.SynchronousPromise,
_argumentsToArray = sut._argumentsToArray;
SynchronousPromise = sut.SynchronousPromise;
describe("synchronous-promise", function () {
it("should be constructable", function () {
expect(SynchronousPromise).to.exist;
expect(new SynchronousPromise(function () { })).to.exist;
expect(new SynchronousPromise(function () {
})).to.exist;
});

@@ -23,29 +23,37 @@ it("should have a then function", function () {

});
function create(ctor) {
return new SynchronousPromise(ctor);
}
function createResolved(data) {
return SynchronousPromise.resolve(data);
}
function createRejected(data) {
return SynchronousPromise.reject(data);
}
describe("then", function () {
it("when resolved, should return a new resolved promise", function () {
var sut = createResolved();
expect(sut.then(null, function () { })).to.be.instanceOf(SynchronousPromise);
const sut = createResolved();
expect(sut.then(null, function () {
})).to.be.instanceOf(SynchronousPromise);
});
it("should return the new resolved promise v2", function () {
var
result = createResolved().then(function () {
/* purposely don't return anything */
});
const result = createResolved().then(function () {
/* purposely don't return anything */
});
expect(result).to.be.instanceOf(SynchronousPromise);
});
it("should return a new rejected promise", function () {
var sut = createRejected();
expect(sut.then(function () { })).to.be.instanceOf(SynchronousPromise);
const sut = createRejected();
expect(sut.then(function () {
})).to.be.instanceOf(SynchronousPromise);
});
it("should return a new rejected promise v2", function () {
var result = createRejected().then(function () {
const result = createRejected().then(function () {
/* purposely don't return anything */

@@ -55,6 +63,6 @@ });

});
it("should bring the first resolve value into the first then", function () {
var
initial = "123",
captured = null;
const initial = "123";
let captured = null;
createResolved(initial).then(function (data) {

@@ -67,6 +75,5 @@ captured = data;

it("should call into the immediate catch function when the function given to then throws", function () {
var
sut = createResolved(),
expected = "the error",
received = null;
const sut = createResolved(),
expected = "the error";
let received = null;
sut.then(function () {

@@ -82,7 +89,6 @@ throw new Error(expected);

// Arrange
var
sut = createResolved(),
error1 = "moo",
received = null,
expected = "moo-cow";
const sut = createResolved(),
error1 = "moo";
let received = null;
const expected = "moo-cow";
// Act

@@ -103,10 +109,11 @@ sut.then(function () {

// Arrange
var
expected = "moo",
const expected = "moo",
sut = new SynchronousPromise(function (resolve, reject) {
reject(expected);
}),
captured;
});
let captured = null;
// Act
sut.catch(function (e) { captured = e; });
sut.catch(function (e) {
captured = e;
});
// Assert

@@ -117,6 +124,5 @@ expect(captured).to.equal(expected);

it("should call into the later catch function when the function given to then throws", function () {
var
sut = createResolved(),
expected = "the error",
received = null;
const sut = createResolved(),
expected = "the error";
let received = null;
sut.then(function () {

@@ -134,6 +140,5 @@ throw new Error(expected);

it("should prefer to call into onRejected over the .catch handler on failure", function () {
var
sut = createResolved(),
expected = "the error",
captured = null,
const sut = createResolved(),
expected = "the error";
let captured = null,
catchCaptured = null;

@@ -154,5 +159,4 @@ sut.then(function () {

it("should bring the first rejected value into the first onRejected then handler", function () {
var
initial = new Error("123"),
captured = null;
const initial = new Error("123");
let captured = null;
createRejected(initial).then(function () {

@@ -166,5 +170,4 @@ }, function (e) {

it("should resolve when the first resolution is a resolved promise", function () {
var
initial = createResolved("123"),
captured = null;
const initial = createResolved("123");
let captured = null;
createResolved(initial).then(function (data) {

@@ -175,6 +178,6 @@ captured = data;

});
it("should catch when the first resolution is a rejected promise", function () {
var
initial = createRejected("123"),
captured = null;
const initial = createRejected("123");
let captured = null;
createResolved(initial).catch(function (data) {

@@ -184,8 +187,8 @@ captured = data;

expect(captured).to.equal("123");
})
});
it("should catch when a subsequent resolution returns a rejected promise", function () {
var
initial = createResolved("123"),
captured = null,
expected = "le error";
const initial = createResolved("123");
let captured = null;
const expected = "le error";
initial.then(function () {

@@ -195,12 +198,12 @@ return createRejected(expected);

captured = e;
})
});
expect(captured).to.equal(expected);
});
it("should run a simple chain", function () {
var
initial = "123",
second = "abc",
captured = null;
createResolved(initial).then(function (data) {
const initial = "123",
second = "abc";
let captured = null;
createResolved(initial).then(function () {
return createResolved(second);

@@ -212,12 +215,12 @@ }).then(function (data) {

});
it("should run a longer chain", function () {
var
initial = "123",
const initial = "123",
second = "abc",
third = "---",
expected = second + third,
captured = null;
createResolved(initial).then(function (data) {
expected = second + third;
let captured = null;
createResolved(initial).then(function () {
return createResolved(second);
}).then(function (data) {
}).then(function () {
return second;

@@ -229,12 +232,12 @@ }).then(function (data) {

});
it("should run a longer chain v2", function () {
var
initial = "123",
const initial = "123",
second = "abc",
third = "---",
expected = second + third,
captured = null;
createResolved(initial).then(function (data) {
expected = second + third;
let captured = null;
createResolved(initial).then(function () {
return createResolved(second);
}).then(function (data) {
}).then(function () {
return createResolved(second);

@@ -246,12 +249,12 @@ }).then(function (data) {

});
it("should run a longer chain v3", function () {
var
initial = "123",
const initial = "123",
second = "abc",
third = "---",
expected = second + third,
captured = null;
createResolved(initial).then(function (data) {
expected = second + third;
let captured = null;
createResolved(initial).then(function () {
return second;
}).then(function (data) {
}).then(function () {
return createResolved(second);

@@ -263,8 +266,8 @@ }).then(function (data) {

});
it("should resolve when the ctor function resolves", function () {
var
providedResolve = null,
captured = null,
expected = "xyz",
promise = create(function (resolve, reject) {
let providedResolve = null,
captured = null;
const expected = "xyz",
promise = create(function (resolve) {
providedResolve = resolve;

@@ -274,6 +277,6 @@ }).then(function (data) {

});
expect(promise).to.exist;
expect(captured).to.be.null;
expect(providedResolve).to.be.a("function");
providedResolve(expected)
providedResolve(expected);
expect(captured).to.equal(expected);

@@ -284,7 +287,6 @@ });

// Arrange
var
expected = "multi-pass",
sut = SynchronousPromise.resolve(expected),
captured1,
captured2;
const expected = "multi-pass",
sut = SynchronousPromise.resolve(expected);
let captured1 = null,
captured2 = null;
// Act

@@ -298,17 +300,17 @@ sut.then(result => captured1 = result);

});
describe("catch", function () {
it("should be called if the initial reject is called", function () {
var
expected = "123",
captured = null;
const expected = "123";
let captured = null;
createRejected(expected).catch(function (e) {
captured = e;
})
});
expect(captured).to.equal(expected);
});
it("should call handler if the promise resolves", function () {
// Arrange
var
sut = SynchronousPromise.unresolved(),
resolved = false,
const sut = SynchronousPromise.unresolved();
let resolved = false,
caught = false;

@@ -322,7 +324,7 @@ // Act

});
it("should be called on a delayed rejection", function () {
var
providedReject = null,
captured = null,
expected = "123",
let providedReject = null,
captured = null;
const expected = "123",
promise = create(function (resolve, reject) {

@@ -333,3 +335,3 @@ providedReject = reject;

});
expect(promise).to.exist;
expect(captured).to.be.null;

@@ -340,5 +342,5 @@ expect(providedReject).to.be.a("function");

});
it("should return a resolved promise if doesn't throw an error", function () {
var
promise = createRejected("123"),
const promise = createRejected("123"),
result = promise.catch(function (data) {

@@ -352,7 +354,7 @@ expect(data).to.equal("123");

});
it("should not interfere with a later then if there is no error", function () {
var
captured = null,
expected = "123",
capturedError = null;
let captured = null;
const expected = "123";
let capturedError = null;
createResolved(expected).catch(function (e) {

@@ -362,3 +364,3 @@ capturedError = e;

captured = data;
})
});

@@ -368,9 +370,10 @@ expect(capturedError).to.be.null;

});
it("should not be called if the promise is handled successful by a previous onRejected handler", function () {
var
expected = new Error("123"),
notExpected = new Error("Not expected"),
capturedError = null;
const expected = new Error("123"),
notExpected = new Error("Not expected");
let capturedError = null;
createRejected(expected).then(
function () { },
function () {
},
function (e) {

@@ -382,15 +385,15 @@ capturedError = e

capturedError = notExpected;
})
});
expect(capturedError).to.equal(expected);
});
it("should prevent the handlers after the error from being called", function () {
var
captured = null;
let captured = null;
createResolved("123").catch(function (e) {
}).then(function (data) {
}).then(function () {
throw "foo";
}).then(function (data) {
}).then(function () {
captured = "abc";
})
});

@@ -402,6 +405,5 @@ expect(captured).to.be.null;

// Arrange
var
expected = "123",
pausedRejectedPromise = SynchronousPromise.reject(expected).pause(),
capturedA = null,
const expected = "123",
pausedRejectedPromise = SynchronousPromise.reject(expected).pause();
let capturedA = null,
capturedB = null;

@@ -415,3 +417,3 @@

capturedB = e;
})
});

@@ -435,6 +437,5 @@ // Act

// Arrange
var
expected = "123",
pausedRejectedPromise = SynchronousPromise.reject(expected).pause(),
capturedA = null,
const expected = "123",
pausedRejectedPromise = SynchronousPromise.reject(expected).pause();
let capturedA = null,
capturedB = null;

@@ -450,3 +451,3 @@

capturedB = e;
})
});

@@ -466,6 +467,5 @@ // Act

// Arrange
var
expected = "123",
pausedRejectedPromise = SynchronousPromise.reject(expected).pause(),
capturedA = null,
const expected = "123",
pausedRejectedPromise = SynchronousPromise.reject(expected).pause();
let capturedA = null,
capturedB = null,

@@ -500,2 +500,3 @@ secondResolve;

});
describe("prototype pause", function () {

@@ -505,2 +506,3 @@ it("should exist as a function on the prototype", function () {

});
it("should return the promise", function () {

@@ -512,4 +514,5 @@ const

});
it("should prevent resolution from continuing at that point", function () {
var calls = 0;
let calls = 0;
createResolved("123").then(function () {

@@ -522,12 +525,13 @@ return calls++;

});
it("should prevent rejection from being caught at that point", function () {
var calls = 0;
createRejected("123").pause().catch(function (e) {
let calls = 0;
createRejected("123").pause().catch(function () {
calls++;
})
});
expect(calls).to.equal(0);
});
it("should prevent rejection from continuing past at that point", function () {
var
calls = 0,
let calls = 0,
captured = null;

@@ -546,12 +550,12 @@

expect(calls).to.equal(0);
})
});
describe("starting paused", function () {
it("should return a promise in paused state with no initial data and being resolved on resume", function () {
var
captured,
promise = SynchronousPromise.resolve().pause().then(function () {
return "moo";
}).then(function (data) {
captured = data;
});
let captured = undefined;
const promise = SynchronousPromise.resolve().pause().then(function () {
return "moo";
}).then(function (data) {
captured = data;
});
expect(captured).to.be.undefined;

@@ -561,6 +565,6 @@ promise.resume();

});
it("should return a promise in paused state with no initial data and being rejected on resume", function () {
var
captured,
expected = new Error("moon"),
let captured = undefined;
const expected = new Error("moon"),
promise = SynchronousPromise.resolve().pause().then(function () {

@@ -575,6 +579,6 @@ throw expected

});
it("should return a promise in paused state with no initial data and being resolved after a catch on resume", function () {
var
captured,
error = new Error("moon"),
let captured = undefined;
const error = new Error("moon"),
promise = SynchronousPromise.resolve().pause().then(function () {

@@ -597,22 +601,23 @@ throw error

});
it("should return the promise", function () {
var
promise = createResolved("123").pause(),
const promise = createResolved("123").pause(),
result = promise.resume();
expect(result).to.equal(promise);
});
it("should not barf if the promise is not already paused", function () {
var promise = createResolved("123");
const promise = createResolved("123");
expect(function () {
promise.resume();
}).not.to.throw;
})
});
it("should resume resolution operations after the last pause", function () {
var
calls = 0,
promise = createResolved("123").then(function () {
return calls++;
}).pause().then(function () {
return calls++;
});
let calls = 0;
const promise = createResolved("123").then(function () {
return calls++;
}).pause().then(function () {
return calls++;
});
expect(calls).to.equal(1);

@@ -622,7 +627,7 @@ promise.resume();

});
it("should resume rejection operations after the last pause", function () {
var
calls = 0,
captured = null,
expected = "die, scum!",
let calls = 0,
captured = null;
const expected = "die, scum!",
promise = createResolved("123").then(function () {

@@ -641,7 +646,7 @@ throw expected;

});
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!",
let calls = 0,
captured = null;
const expected = "it\"s the end of the world!",
promise = SynchronousPromise.reject(expected).pause().then(function () {

@@ -657,4 +662,4 @@ calls++;

expect(captured).to.equal(expected);
})
})
});
});
describe("static resolve", function () {

@@ -664,8 +669,8 @@ it("should be a function", function () {

});
it("should return a resolved promise", function () {
var
expected = "foo",
const expected = "foo",
result = SynchronousPromise.resolve(expected);
expect(result.status).to.equal("resolved");
var captured = null;
let captured = null;
result.then(function (data) {

@@ -675,3 +680,3 @@ captured = data;

expect(captured).to.equal(expected);
})
});
});

@@ -682,8 +687,8 @@ describe("static reject", function () {

});
it("should return a rejected promise", function () {
var
expected = "moo",
const expected = "moo",
result = SynchronousPromise.reject(expected);
expect(result.status).to.equal("rejected");
var captured = null;
let captured = null;
result.catch(function (err) {

@@ -695,12 +700,13 @@ captured = err;

});
describe("static all", function () {
it("should be a function", function () {
expect(SynchronousPromise.all).to.be.a("function")
})
});
it("should resolve with all values from given resolved promises as variable args", function () {
var
p1 = createResolved("abc"),
const p1 = createResolved("abc"),
p2 = createResolved("123"),
all = SynchronousPromise.all(p1, p2),
captured = null;
all = SynchronousPromise.all(p1, p2);
let captured = null;

@@ -717,5 +723,4 @@ all.then(function (data) {

it("should resolve with all values from given promise or none promise variable args", function () {
var
all = SynchronousPromise.all(["123", createResolved("abc")]),
captured = null;
const all = SynchronousPromise.all(["123", createResolved("abc")]);
let captured = null;

@@ -730,8 +735,8 @@ all.then(function (data) {

});
it("should resolve with all values from given resolved promises as an array", function () {
var
p1 = createResolved("abc"),
const p1 = createResolved("abc"),
p2 = createResolved("123"),
all = SynchronousPromise.all([p1, p2]),
captured = null;
all = SynchronousPromise.all([p1, p2]);
let captured = null;

@@ -746,6 +751,6 @@ all.then(function (data) {

});
it("should resolve empty promise array", function () {
var
all = SynchronousPromise.all([]),
captured = null;
const all = SynchronousPromise.all([]);
let captured = null;

@@ -758,13 +763,13 @@ all.then(function (data) {

});
it("should resolve with values in the correct order", function () {
var
resolve1,
resolve2,
captured;
let resolve1 = undefined,
resolve2 = undefined,
captured = undefined;
var p1 = create(function (resolve) {
const p1 = create(function (resolve) {
resolve1 = resolve;
});
var p2 = create(function (resolve) {
const p2 = create(function (resolve) {
resolve2 = resolve;

@@ -782,8 +787,8 @@ });

});
it("should reject if any promise rejects", function () {
var
p1 = createResolved("abc"),
const p1 = createResolved("abc"),
p2 = createRejected("123"),
all = SynchronousPromise.all(p1, p2),
capturedData = null,
all = SynchronousPromise.all(p1, p2);
let capturedData = null,
capturedError = null;

@@ -807,6 +812,7 @@ all.then(function (data) {

});
it("should return a new SynchronousPromise", function () {
// Arrange
// Act
var result1 = SynchronousPromise.unresolved(),
const result1 = SynchronousPromise.unresolved(),
result2 = SynchronousPromise.unresolved();

@@ -820,6 +826,7 @@ // Assert

});
describe("result", function () {
it("should not be resolved or rejected", function () {
// Arrange
var resolved = false,
let resolved = false,
rejected = false;

@@ -836,2 +843,3 @@ // Act

});
describe("resolve", function () {

@@ -841,3 +849,3 @@ it("should be a function", function () {

// Act
var sut = SynchronousPromise.unresolved();
const sut = SynchronousPromise.unresolved();
// Assert

@@ -847,8 +855,8 @@ expect(sut.resolve).to.exist;

});
it("should resolve the promise when invoked", function () {
// Arrange
var
resolved = undefined,
error = undefined,
sut = SynchronousPromise.unresolved().then(function (result) {
let resolved = undefined,
error = undefined;
const sut = SynchronousPromise.unresolved().then(function (result) {
resolved = result;

@@ -858,3 +866,3 @@ }).catch(function (err) {

}),
expected = { key: "value" };
expected = {key: "value"};
// Act

@@ -867,11 +875,13 @@ debugger;

});
it("should resolve all thens when invoked", () => {
// Arrange
var
sut = SynchronousPromise.unresolved(),
captured1,
captured2,
next1 = sut.then(result => captured1 = result),
const sut = SynchronousPromise.unresolved();
let captured1 = undefined,
captured2 = undefined;
const next1 = sut.then(result => captured1 = result),
next2 = sut.then(result => captured2 = result),
expected = "cake-moo";
expect(next1).to.exist;
expect(next2).to.exist;
// Act

@@ -888,3 +898,3 @@ sut.resolve(expected);

// Act
var sut = SynchronousPromise.unresolved();
const sut = SynchronousPromise.unresolved();
// Assert

@@ -894,8 +904,8 @@ expect(sut.reject).to.exist;

});
it("should reject the promise when invoked", function () {
// Arrange
var
resolved = undefined,
error = undefined,
sut = SynchronousPromise.unresolved().then(function (result) {
let resolved = undefined,
error = undefined;
const sut = SynchronousPromise.unresolved().then(function (result) {
resolved = result;

@@ -905,3 +915,3 @@ }).catch(function (err) {

}),
expected = { key: "value" };
expected = {key: "value"};
// Act

@@ -918,14 +928,13 @@ sut.reject(expected);

// Arrange
var
captured,
sut = new SynchronousPromise(function(resolve, reject) {
setTimeout(function() {
resolve("moo");
}, 0);
}).then(function(result) {
captured = result;
});
let captured;
// Act
new SynchronousPromise(function (resolve) {
setTimeout(function () {
resolve("moo");
}, 0);
}).then(function (result) {
captured = result;
});
// Assert
setTimeout(function() {
setTimeout(function () {
expect(captured).to.equal("moo");

@@ -941,5 +950,5 @@ done();

// Arrange
var called = false;
let called = false;
// Act
SynchronousPromise.resolve("foo").finally(function() {
SynchronousPromise.resolve("foo").finally(function () {
called = true;

@@ -950,7 +959,8 @@ });

});
it(`should call the provided function when the promise rejects`, async () => {
// Arrange
var called = false;
let called = false;
// Act
SynchronousPromise.reject("foo").finally(function() {
SynchronousPromise.reject("foo").finally(function () {
called = true;

@@ -960,12 +970,12 @@ });

});
it(`should call the provided function when the promise is rejected then caught`, async () => {
// Arrange
var
catchCalled = false,
let catchCalled = false,
finallyCalled = false;
// Act
SynchronousPromise.reject("error")
.catch(function(e) {
catchCalled = true;
}).finally(function() {
.catch(function () {
catchCalled = true;
}).finally(function () {
finallyCalled = true;

@@ -977,88 +987,94 @@ });

});
it(`should start a new promise chain after resolution, with non-throwing finally`, async () => {
// Arrange
var captured = null;
let captured = null;
// Act
SynchronousPromise.resolve("first value")
.finally(function() {
.finally(function () {
return "second value";
}).then(function(data) {
captured = data;
});
}).then(function (data) {
captured = data;
});
// Assert
expect(captured).to.equal("second value");
});
it(`should start a new promise chain after resolution, with resolving finally`, async () => {
// Arrange
var captured = null;
let captured = null;
// Act
SynchronousPromise.resolve("first value")
.finally(function() {
.finally(function () {
return SynchronousPromise.resolve("second value");
}).then(function(data) {
captured = data;
});
}).then(function (data) {
captured = data;
});
// Assert
expect(captured).to.equal("second value");
});
it(`should start a new promise chain after resolution, with throwing finally`, async () => {
// Arrange
var captured = null;
let captured = null;
// Act
SynchronousPromise.reject("first error")
.finally(function() {
.finally(function () {
throw "finally data";
}).catch(function(data) {
captured = data;
});
}).catch(function (data) {
captured = data;
});
// Assert
expect(captured).to.equal("finally data");
});
it(`should start a new promise chain after resolution, with rejecting finally`, async () => {
// Arrange
var captured = null;
let captured = null;
// Act
SynchronousPromise.reject("first error")
.finally(function() {
.finally(function () {
return SynchronousPromise.reject("finally data");
}).catch(function(data) {
captured = data;
});
}).catch(function (data) {
captured = data;
});
// Assert
expect(captured).to.equal("finally data");
});
it(`should start a new promise chain after rejection, with non-throwing finally`, async () => {
// Arrange
var
called = false;
let called = false;
// Act
SynchronousPromise.reject("le error")
.finally(function() {
}).then(function() {
called = true;
});
.finally(function () {
}).then(function () {
called = true;
});
// Assert
expect(called).to.be.true;
});
it(`should start a new promise chain after rejection, with resolving finally`, async () => {
// Arrange
var captured = null;
let captured = null;
// Act
SynchronousPromise.reject("le error")
.finally(function() {
.finally(function () {
return SynchronousPromise.resolve("le data");
}).then(function(data) {
captured = data;
});
}).then(function (data) {
captured = data;
});
// Assert
expect(captured).to.equal("le data");
});
it(`should start a new promise chain after rejection, with throwing finally`, async () => {
// Arrange
var finallyError = null;
let finallyError = null;
// Act
SynchronousPromise.reject("another error")
.finally(function() {
throw "moo cakes";
}).catch(function(err) {
.finally(function () {
throw "moo cakes";
}).catch(function (err) {
finallyError = err;

@@ -1069,10 +1085,11 @@ });

});
it(`should start a new promise chain after rejection, with rejecting finally`, async () => {
// Arrange
var finallyError = null;
let finallyError = null;
// Act
SynchronousPromise.reject("another error")
.finally(function() {
return SynchronousPromise.reject("moo cakes");
}).catch(function(err) {
.finally(function () {
return SynchronousPromise.reject("moo cakes");
}).catch(function (err) {
finallyError = err;

@@ -1083,3 +1100,372 @@ });

});
describe(`issues`, () => {
it(`should be called after one then from resolved()`, async () => {
// Arrange
const events = [];
// Act
SynchronousPromise.resolve("initial")
.then(result => {
events.push(result);
events.push("then");
}).finally(() => {
events.push("finally");
});
// Assert
expect(events).to.eql(
["initial", "then", "finally"]
);
});
it(`should be called after two thens from resolved()`, async () => {
// Arrange
const events = [];
// Act
SynchronousPromise.resolve("initial")
.then(result => {
events.push(result);
events.push("then1");
return "then1";
}).then(result => {
events.push(`then2 received: ${result}`);
events.push("then2");
console.log(events);
}).finally(() => {
events.push("finally");
});
// Assert
expect(events).to.eql(
[ "initial", "then1", "then2 received: then1", "then2", "finally" ]
);
});
it(`should not be called from an unresolved promise`, async () => {
// Arrange
const events = [];
// Act
SynchronousPromise.unresolved()
.then(result => {
debugger;
events.push(`result: ${result}`)
})
.catch(() => events.push("catch"))
.finally(() => events.push("finally"));
// Assert
expect(events).to.be.empty;
});
it(`should not be run if chain is paused`, async () => {
// Arrange
const events = [];
const promise = SynchronousPromise.resolve('init')
.then((result) => { events.push(`result: ${result}`) })
.pause()
.then(() => { events.push('resumed') })
.finally(() => { events.push('finally') });
expect(events).to.eql([ "result: init"]);
// Act
promise.resume();
// Assert
expect(events).to.eql([ "result: init", "resumed", "finally" ]);
});
describe(`imported specs from blalasaadri`, () => {
// these relate to https://github.com/fluffynuts/synchronous-promise/issues/15
// reported by https://github.com/blalasaadri
describe('SynchronousPromise', () => {
describe('new SynchronousPromise', () => {
it('calls .then() after being resolved', () => {
const events = [];
new SynchronousPromise((resolve) => {
events.push('init');
resolve('resolve')
}).then(result => { events.push(`result: ${result}`) })
.then(() => { events.push('then') });
expect(events)
.to.eql(['init', 'result: resolve', 'then'])
});
it('calls .catch() but not previous .then()s after being rejected', () => {
const events = [];
new SynchronousPromise((resolve, reject) => {
events.push('init');
reject('reject')
}).then(result => { events.push(`result: ${result}`) })
.then(() => { events.push('then') })
.catch(error => { events.push(`error: ${error}`) });
expect(events)
.to.eql(['init', 'error: reject'])
});
it('calls .finally() after .then()', () => {
const events = [];
new SynchronousPromise((resolve) => {
resolve('init')
}).then(result => { events.push(`result: ${result}`) })
.then(() => { events.push('then') })
.finally(() => { events.push('finally') });
expect(events)
.to.eql(['result: init', 'then', 'finally'])
});
it('calls .finally() after .catch()', () => {
const events = [];
new SynchronousPromise((resolve, reject) => {
reject('init')
}).then(result => { events.push(`result: ${result}`) })
.then(() => { events.push('then') })
.catch(error => { events.push(`error: ${error}`) })
.finally(() => { events.push('finally') });
expect(events)
.to.eql(['error: init', 'finally'])
})
});
describe('SynchronousPromise.unresolved', () => {
describe('calls .then() only after being resolved', () => {
it('calls nothing before promise.resolve is called', () => {
const events = [];
SynchronousPromise.unresolved()
.then((result) => { events.push(`result: ${result}`) })
.then(() => { events.push('then') });
expect(events).to.eql([])
});
it('calls .then() once promise.resolve is called', () => {
const events = [];
const promise = SynchronousPromise.unresolved()
.then((result) => { events.push(`result: ${result}`) })
.then(() => { events.push('then') });
promise.resolve('resolve');
expect(events).to.eql(['result: resolve', 'then'])
})
});
it('calls .catch() but not previous .then()s after being rejected', () => {
const events = [];
const promise = SynchronousPromise.unresolved()
.then((result) => { events.push(`result: ${result}`) })
.then(() => { events.push('then') })
.catch(error => { events.push(`error: ${error}`) });
promise.reject('reject');
expect(events)
.to.eql(['error: reject'])
});
describe('calls .finally() after .then()', () => {
it('calls nothing before promise.resolve is called', () => {
const events = [];
SynchronousPromise.unresolved()
.then((result) => { events.push(`result: ${result}`) })
.then(() => { events.push('then') })
.finally(() => { events.push('finally') });
expect(events)
.not.to.contain('finally');
expect(events)
.to.eql([])
});
it('calls .then() and .finally() once promise.resolve is called', () => {
const events = [];
const promise = SynchronousPromise.unresolved()
.then((result) => { events.push(`result: ${result}`) })
.then(() => { events.push('then') })
.finally(() => { events.push('finally') });
promise.resolve('resolve');
expect(events)
.not.to.eql(['finally', 'result: undefined', 'then']);
expect(events)
.to.eql(['result: resolve', 'then', 'finally'])
})
});
describe('calls .finally() after .catch()', () => {
it('calls nothing before promise.reject is called', () => {
const events = [];
SynchronousPromise.unresolved()
.then((result) => { events.push(`result: ${result}`) })
.catch(() => { events.push('catch') })
.finally(() => { events.push('finally') });
expect(events)
.not.to.contain('finally');
expect(events)
.to.eql([])
});
it('calls .catch() and .finally() once promise.reject is called', () => {
const events = [];
const promise = SynchronousPromise.unresolved()
.then((result) => { events.push(`result: ${result}`) })
.catch((error) => { events.push(`error: ${error}`) })
.finally(() => { events.push('finally') });
promise.reject('reject');
expect(events)
.not.to.eql(['finally', 'result: undefined']);
expect(events)
.to.eql(['error: reject', 'finally'])
})
})
});
describe('SynchronousPromise.resolve(...).pause', () => {
describe('calls .then() only after being resolved', () => {
it('calls nothing after the initial initialization before promise.resume is called', () => {
const events = [];
SynchronousPromise.resolve('init')
.then((result) => { events.push(`result: ${result}`) })
.pause()
.then(() => { events.push('resumed') });
expect(events)
.to.eql(['result: init'])
});
it('calls .then() after the inital initialization after promise.resume is called', () => {
const events = [];
const promise = SynchronousPromise.resolve('init')
.then((result) => { events.push(`result: ${result}`) })
.pause()
.then(() => { events.push('resumed') });
promise.resume();
expect(events)
.to.eql(['result: init', 'resumed'])
})
});
describe('calls .catch() only after being resolved', () => {
it('calls nothing after the inital initialization before promise.resume is called', () => {
const events = [];
SynchronousPromise.resolve('init')
.then((result) => {
events.push(`result: ${result}`);
throw Error('resumed')
})
.pause()
.catch(({ message }) => { events.push(`catch: ${message}`) });
expect(events)
.to.eql(['result: init'])
});
it('calls .catch() after the inital initialization after promise.resume is called', () => {
const events = [];
const promise = SynchronousPromise.resolve('init')
.then((result) => {
events.push(`result: ${result}`);
throw Error('resumed')
})
.pause()
.catch(({ message }) => { events.push(`catch: ${message}`) });
promise.resume();
expect(events)
.to.eql(['result: init', 'catch: resumed'])
})
});
describe('calls .finally() after .then()', () => {
it('calls nothing before promise.resume is called', () => {
const events = [];
SynchronousPromise.resolve('init')
.then((result) => { events.push(`result: ${result}`) })
.pause()
.then(() => { events.push('resumed') })
.finally(() => { events.push('finally') });
expect(events)
.not.to.contain('finally');
expect(events)
.to.eql(['result: init'])
});
it('calls .then() and .finally() once promise.resume is called', () => {
const events = [];
const promise = SynchronousPromise.resolve('init')
.then((result) => { events.push(`result: ${result}`) })
.pause()
.then(() => { events.push('resumed') })
.finally(() => { events.push('finally') });
promise.resume();
expect(events)
.not.to.eql(['result: init', 'finally', 'resumed']);
expect(events)
.to.eql(['result: init', 'resumed', 'finally'])
})
});
describe('calls .finally() after .catch()', () => {
it('calls nothing before promise.resume is called', () => {
const events = [];
SynchronousPromise.resolve('init')
.then((result) => {
events.push(`result: ${result}`);
throw Error('resumed')
})
.pause()
.catch(({ message }) => { events.push(`catch: ${message}`) })
.finally(() => { events.push('finally') });
expect(events)
.not.to.contain('finally');
expect(events)
.to.eql(['result: init'])
});
it('calls .catch() and .finally() once promise.resume is called', () => {
const events = [];
const promise = SynchronousPromise.resolve('init')
.then((result) => {
events.push(`result: ${result}`);
throw Error('resumed')
})
.pause()
.catch(({ message }) => { events.push(`catch: ${message}`) })
.finally(() => { events.push('finally') });
promise.resume();
expect(events)
.not.to.eql(['result: init', 'finally', 'catch: resumed']);
expect(events)
.to.eql(['result: init', 'catch: resumed', 'finally'])
})
})
})
})
});
});
});
});
{
"name": "synchronous-promise",
"version": "2.0.9",
"version": "2.0.10",
"description": "Synchronous Promise-like prototype to use in testing where you would have used an ES6 Promise",

@@ -45,3 +45,2 @@ "main": "index.js",

"mocha-yar": "^1.0.13",
"node-ts": "^2.1.2",
"nodemon": "^1.18.4",

@@ -48,0 +47,0 @@ "npm-run-all": "^4.1.2",

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc