Sven Slootweg a44d2782bf | 4 months ago | |
---|---|---|
.gitignore | 4 months ago | |
README.md | 4 months ago | |
example.js | 4 months ago | |
index.js | 4 months ago | |
package.json | 4 months ago |
README.md
promise-defer
Small utility function to create a defer; that is, a Promise which can be externally resolved or rejected.
You should not use this unless you have no other option. Typically that means you are implementing an asynchronous queue of some sort. This is not an appropriate tool to use for typical asynchronous/Promises code; not even for most cases where you need to promisify something - in those cases you should use new Promise
instead.
This module should strictly be used for cases where the resolution and rejection have to be implemented separately from the Promise itself due to architectural constraints that cannot be solved otherwise, and you need to make sure that you fully understand the implications that this has for eg. error handling. If you're not sure what this means, you should not use this module.
Example
"use strict";
const promiseDefer = require("@joepie91/promise-defer");
let { resolve, reject, promise } = promiseDefer();
(async function () {
await promise;
console.log("resolved!");
})();
setTimeout(() => {
resolve();
}, 1000);
API
promiseDefer()
Returns: An object with { promise, resolve, reject }
keys. The promise
is the generated Promise for this defer, whereas resolve
and reject
are respectively the functions for resolving and rejecting that Promise.