sindresorhus / P Lazy
Licence: mit
Create a lazy promise that defers execution until it's awaited or when .then() or .catch() is called
Stars: ✭ 222
Programming Languages
javascript
184084 projects - #8 most used programming language
p-lazy
Create a lazy promise that defers execution until it's awaited or when
.then()
or.catch()
is called
Useful if you're doing some heavy operations and would like to only do it when the promise is actually used.
Install
$ npm install p-lazy
Usage
const PLazy = require('p-lazy');
const lazyPromise = new PLazy(resolve => {
someHeavyOperation(resolve);
});
// `someHeavyOperation` is not yet called
(async () => {
await doSomethingFun;
// `someHeavyOperation` is called
console.log(await lazyPromise);
})();
API
new PLazy(executor)
Same as the Promise
constructor. PLazy
is a subclass of Promise
.
PLazy.from(fn)
Create a PLazy
promise from a promise-returning or async function.
PLazy.resolve(value)
Create a PLazy
promise that is resolved with the given value
, or the promise passed as value
.
PLazy.reject(reason)
Create a PLazy
promise that is rejected with the given reason
.
Related
- p-cancelable - Create a promise that can be canceled
- p-defer - Create a deferred promise
- lazy-value - Create a lazily evaluated value
- define-lazy-prop - Define a lazily evaluated property on an object
- More…
License
MIT © Sindre Sorhus
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].