Initial version
commit
5521a58a9f
@ -0,0 +1 @@
|
||||
node_modules
|
@ -0,0 +1,33 @@
|
||||
# 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
|
||||
|
||||
```js
|
||||
"use strict";
|
||||
|
||||
const promiseDefer = require("./");
|
||||
|
||||
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.
|
@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
const promiseDefer = require("./");
|
||||
|
||||
let { resolve, reject, promise } = promiseDefer();
|
||||
|
||||
(async function () {
|
||||
await promise;
|
||||
console.log("resolved!");
|
||||
})();
|
||||
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, 1000);
|
@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function promiseDefer() {
|
||||
let rej_, res_;
|
||||
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
rej_ = reject;
|
||||
res_ = resolve;
|
||||
});
|
||||
|
||||
return {
|
||||
resolve: res_,
|
||||
reject: rej_,
|
||||
promise: promise
|
||||
};
|
||||
};
|
@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "@joepie91/promise-defer",
|
||||
"version": "1.0.0",
|
||||
"description": "Small utility function for creating a defer",
|
||||
"main": "index.js",
|
||||
"files": [
|
||||
"index.js",
|
||||
"example.js",
|
||||
"README.md"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": ["promise", "async", "defer"],
|
||||
"author": "Sven Slootweg <admin@cryto.net>",
|
||||
"license": "WTFPL OR CC0-1.0"
|
||||
}
|
Loading…
Reference in New Issue