-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
33 lines (28 loc) · 798 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
function generate(willResolve) {
return function (ms, value) {
ms = ms || 0;
// If supplied, the thunk will override promise results with `value`.
var useValue = arguments.length > 1;
var promise = thunk(value);
thunk.then = promise.then.bind(promise);
thunk.catch = promise.catch.bind(promise);
thunk._actualPromise = promise;
return thunk;
function thunk(result) {
if (promise) {
// Prevent unhandled rejection errors if promise is never used, and only used as a thunk
promise.catch(function () {});
}
return new Promise(function (resolve, reject) {
setTimeout(
willResolve ? resolve : reject,
ms,
useValue ? value : result
);
});
}
};
}
module.exports = generate(true);
module.exports.reject = generate(false);