-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b0d22a
commit 90d16f2
Showing
4 changed files
with
57 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,48 @@ | ||
'use strict'; | ||
const defer = require('p-defer'); | ||
|
||
class CancelError extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = 'CancelError'; | ||
} | ||
} | ||
|
||
const generate = willResolve => function (ms, value) { | ||
ms = ms || 0; | ||
const useValue = (arguments.length > 1); | ||
let result = value; | ||
|
||
// If supplied, the thunk will override promise results with `value`. | ||
const useValue = arguments.length > 1; | ||
const delaying = defer(); | ||
const promise = delaying.promise; | ||
|
||
let promise; | ||
let timeout = setTimeout(() => { | ||
const settle = willResolve ? delaying.resolve : delaying.reject; | ||
settle(result); | ||
}, ms); | ||
|
||
const thunk = result => { | ||
if (promise) { | ||
// Prevent unhandled rejection errors if promise is never used, and only used as a thunk | ||
promise.catch(() => {}); | ||
const thunk = thunkResult => { | ||
if (!useValue) { | ||
result = thunkResult; | ||
} | ||
return new Promise((resolve, reject) => { | ||
setTimeout( | ||
willResolve ? resolve : reject, | ||
ms, | ||
useValue ? value : result | ||
); | ||
}); | ||
return promise; | ||
}; | ||
|
||
promise = thunk(value); | ||
thunk.then = promise.then.bind(promise); | ||
thunk.catch = promise.catch.bind(promise); | ||
thunk._actualPromise = promise; | ||
|
||
thunk.cancel = () => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
delaying.reject(new CancelError('Delay canceled')); | ||
} | ||
}; | ||
|
||
return thunk; | ||
}; | ||
|
||
module.exports = generate(true); | ||
module.exports.reject = generate(false); | ||
module.exports.CancelError = CancelError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters