Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

p-lazy

3.0.0 • Public • Published

p-lazy Build Status

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.

Related

License

MIT © Sindre Sorhus

Install

npm i p-lazy

DownloadsWeekly Downloads

106,866

Version

3.0.0

License

MIT

Unpacked Size

4.59 kB

Total Files

5

Last publish

Collaborators

  • avatar