Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
Update bluebird and make cancellable delay work
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie committed May 29, 2018
1 parent c24fda4 commit 81db298
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
20 changes: 14 additions & 6 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,24 @@ function localIp () {
*/
function cancellableDelay (ms) {
let timer;
return new B.Promise((resolve) => {
let resolve;
let reject;

const delay = new B.Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
timer = setTimeout(function () {
resolve();
}, ms);
})
.cancellable()
.catch(B.CancellationError, (err) => { // eslint-disable-line promise/prefer-await-to-callbacks
clearTimeout(timer);
throw err;
});

// override Bluebird's `cancel`, which does not work when using `await` on
// a promise, since `resolve`/`reject` are never called
delay.cancel = function () {
clearTimeout(timer);
reject(new B.CancellationError());
};
return delay;
}

function multiResolve (roots, ...args) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"archiver": "^1.3.0",
"babel-runtime": "=5.8.24",
"bluebird": "^2.9.25",
"bluebird": "^3.5.1",
"bplist-creator": "^0.0.7",
"bplist-parser": "^0.1.0",
"extract-zip": "^1.6.0",
Expand Down
5 changes: 3 additions & 2 deletions test/util-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ describe('util', function () {
});
it('cancel should work', async function () {
let delay = util.cancellableDelay('1000');
B.delay(10).then(() => { delay.cancel(); }).done(); // eslint-disable-line
await delay.should.be.rejectedWith(/cancellation error/);
await B.delay(10);
delay.cancel();
await delay.should.eventually.be.rejectedWith(/cancellation error/);
});
});

Expand Down

0 comments on commit 81db298

Please sign in to comment.