Skip to content

Commit b4b018d

Browse files
Page-medikoo
authored andcommitted
feat(promise): support cancellation case
PR #97 by @Page-
1 parent 040b6d1 commit b4b018d

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

ext/promise.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,17 @@ require("../lib/registered-extensions").promise = function (mode, conf) {
6565
if (!resolvedMode) resolvedMode = "then";
6666

6767
if (resolvedMode === "then") {
68+
var nextTickFailure = function () {
69+
nextTick(onFailure);
70+
};
6871
promise.then(
6972
function (result) { nextTick(onSuccess.bind(this, result)); },
70-
function () { nextTick(onFailure); }
73+
nextTickFailure
7174
);
75+
// If `finally` is a function we attach to it to remove cancelled promises.
76+
if (typeof promise.finally === "function") {
77+
promise.finally(nextTickFailure);
78+
}
7279
} else if (resolvedMode === "done") {
7380
// Not recommended, as it may mute any eventual "Unhandled error" events
7481
if (typeof promise.done !== "function") {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"timers-ext": "^0.1.5"
3636
},
3737
"devDependencies": {
38+
"bluebird": "^3.5.1",
3839
"eslint": "^5.3",
3940
"eslint-config-medikoo-es5": "^1.6",
4041
"plain-promise": "^0.1.1",

test/ext/promise.js

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
/* eslint id-length: 0, handle-callback-err: 0, no-undef: 0, no-unused-vars: 0, func-names: 0 */
1+
/* eslint id-length: 0, handle-callback-err: 0, no-undef: 0, no-unused-vars: 0, func-names: 0,
2+
max-lines: 0 */
23

34
"use strict";
45

56
var memoize = require("../..")
67
, nextTick = require("next-tick")
7-
, Promise = require("plain-promise");
8+
, Promise = require("plain-promise")
9+
, Bluebird = require("bluebird");
10+
11+
Bluebird.config({
12+
cancellation: true
13+
});
814

915
module.exports = function () {
1016
return {
@@ -110,6 +116,90 @@ module.exports = function () {
110116
}, 10);
111117
}
112118
},
119+
"Cancellation": {
120+
Immediate: function (a, d) {
121+
var mfn, fn, i = 0;
122+
fn = function (x, y) {
123+
++i;
124+
var p = new Bluebird(function (res) {
125+
setTimeout(function () {
126+
res(x + y);
127+
}, 100);
128+
});
129+
p.cancel();
130+
return p;
131+
};
132+
133+
mfn = memoize(fn, { promise: true });
134+
135+
mfn(3, 7).done(a.never, function (err) {
136+
a.throws(function () {
137+
throw err;
138+
}, Bluebird.CancellationError, "Result #1");
139+
});
140+
141+
mfn(5, 8).done(a.never, function (err) {
142+
a.throws(function () {
143+
throw err;
144+
}, Bluebird.CancellationError, "Result B #2");
145+
});
146+
147+
setTimeout(function () {
148+
a(i, 2, "Called #2");
149+
150+
mfn(3, 7).done(a.never, function (err) {
151+
a.throws(function () {
152+
throw err;
153+
}, Bluebird.CancellationError, "Again: Result");
154+
});
155+
156+
mfn(5, 8).done(a.never, function (err) {
157+
a.throws(function () {
158+
throw err;
159+
}, Bluebird.CancellationError, "Again B: Result");
160+
});
161+
162+
setTimeout(function (err) {
163+
a(i, 4, "Again Called #2");
164+
d();
165+
}, 10);
166+
}, 10);
167+
},
168+
Delayed: function (a, d) {
169+
var mfn, fn, i = 0;
170+
fn = function (x, y) {
171+
++i;
172+
var p = new Bluebird(function (res) {
173+
setTimeout(function () {
174+
res(x + y);
175+
}, 100);
176+
});
177+
nextTick(function () {
178+
p.cancel();
179+
}, 1);
180+
return p;
181+
};
182+
183+
mfn = memoize(fn, { promise: true });
184+
185+
mfn(3, 7).done(a.never, a.never);
186+
187+
mfn(5, 8).done(a.never, a.never);
188+
189+
setTimeout(function () {
190+
a(i, 2, "Called #2");
191+
192+
mfn(3, 7).done(a.never, a.never);
193+
194+
mfn(5, 8).done(a.never, a.never);
195+
196+
setTimeout(function (err) {
197+
a(i, 4, "Again Called #2");
198+
d();
199+
}, 500);
200+
}, 500);
201+
}
202+
},
113203
"Primitive": {
114204
"Success": function (a, d) {
115205
var mfn, fn, i = 0;

0 commit comments

Comments
 (0)