| 1 |
'use strict'; |
| 2 |
|
| 3 |
function generate(argIndex) { |
| 4 |
return function (ms, value) { |
| 5 |
ms = ms || 0; |
| 6 |
|
| 7 |
// If supplied, the thunk will override promise results with `value`. |
| 8 |
var useValue = arguments.length > 1; |
| 9 |
|
| 10 |
var promise = thunk(value); |
| 11 |
thunk.then = promise.then.bind(promise); |
| 12 |
thunk.catch = promise.catch.bind(promise); |
| 13 |
return thunk; |
| 14 |
|
| 15 |
function thunk(result) { |
| 16 |
if (promise) { |
| 17 |
// Prevent unhandled rejection errors if promise is never used, and only used as a thunk |
| 18 |
promise.catch(function () {}); |
| 19 |
} |
| 20 |
|
| 21 |
return new Promise(function () { |
| 22 |
// resolve / reject |
| 23 |
var complete = arguments[argIndex]; |
| 24 |
|
| 25 |
setTimeout(function () { |
| 26 |
complete(useValue ? value : result); |
| 27 |
}, ms); |
| 28 |
}); |
| 29 |
} |
| 30 |
}; |
| 31 |
} |
| 32 |
|
| 33 |
module.exports = generate(0); |
| 34 |
module.exports.reject = generate(1); |