Skip to content

Commit

Permalink
feat(promise): support cancellation case
Browse files Browse the repository at this point in the history
PR #97 by @Page-
  • Loading branch information
Page- authored and medikoo committed Aug 6, 2018
1 parent 040b6d1 commit b4b018d
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
9 changes: 8 additions & 1 deletion ext/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ require("../lib/registered-extensions").promise = function (mode, conf) {
if (!resolvedMode) resolvedMode = "then";

if (resolvedMode === "then") {
var nextTickFailure = function () {
nextTick(onFailure);
};
promise.then(
function (result) { nextTick(onSuccess.bind(this, result)); },
function () { nextTick(onFailure); }
nextTickFailure
);
// If `finally` is a function we attach to it to remove cancelled promises.
if (typeof promise.finally === "function") {
promise.finally(nextTickFailure);
}
} else if (resolvedMode === "done") {
// Not recommended, as it may mute any eventual "Unhandled error" events
if (typeof promise.done !== "function") {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"timers-ext": "^0.1.5"
},
"devDependencies": {
"bluebird": "^3.5.1",
"eslint": "^5.3",
"eslint-config-medikoo-es5": "^1.6",
"plain-promise": "^0.1.1",
Expand Down
94 changes: 92 additions & 2 deletions test/ext/promise.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/* eslint id-length: 0, handle-callback-err: 0, no-undef: 0, no-unused-vars: 0, func-names: 0 */
/* eslint id-length: 0, handle-callback-err: 0, no-undef: 0, no-unused-vars: 0, func-names: 0,
max-lines: 0 */

"use strict";

var memoize = require("../..")
, nextTick = require("next-tick")
, Promise = require("plain-promise");
, Promise = require("plain-promise")
, Bluebird = require("bluebird");

Bluebird.config({
cancellation: true
});

module.exports = function () {
return {
Expand Down Expand Up @@ -110,6 +116,90 @@ module.exports = function () {
}, 10);
}
},
"Cancellation": {
Immediate: function (a, d) {
var mfn, fn, i = 0;
fn = function (x, y) {
++i;
var p = new Bluebird(function (res) {
setTimeout(function () {
res(x + y);
}, 100);
});
p.cancel();
return p;
};

mfn = memoize(fn, { promise: true });

mfn(3, 7).done(a.never, function (err) {
a.throws(function () {
throw err;
}, Bluebird.CancellationError, "Result #1");
});

mfn(5, 8).done(a.never, function (err) {
a.throws(function () {
throw err;
}, Bluebird.CancellationError, "Result B #2");
});

setTimeout(function () {
a(i, 2, "Called #2");

mfn(3, 7).done(a.never, function (err) {
a.throws(function () {
throw err;
}, Bluebird.CancellationError, "Again: Result");
});

mfn(5, 8).done(a.never, function (err) {
a.throws(function () {
throw err;
}, Bluebird.CancellationError, "Again B: Result");
});

setTimeout(function (err) {
a(i, 4, "Again Called #2");
d();
}, 10);
}, 10);
},
Delayed: function (a, d) {
var mfn, fn, i = 0;
fn = function (x, y) {
++i;
var p = new Bluebird(function (res) {
setTimeout(function () {
res(x + y);
}, 100);
});
nextTick(function () {
p.cancel();
}, 1);
return p;
};

mfn = memoize(fn, { promise: true });

mfn(3, 7).done(a.never, a.never);

mfn(5, 8).done(a.never, a.never);

setTimeout(function () {
a(i, 2, "Called #2");

mfn(3, 7).done(a.never, a.never);

mfn(5, 8).done(a.never, a.never);

setTimeout(function (err) {
a(i, 4, "Again Called #2");
d();
}, 500);
}, 500);
}
},
"Primitive": {
"Success": function (a, d) {
var mfn, fn, i = 0;
Expand Down

0 comments on commit b4b018d

Please sign in to comment.