JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 171250
  • Score
    100M100P100Q162111F
  • License MIT

Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called

Package Exports

  • p-lazy

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (p-lazy) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.

License

MIT © Sindre Sorhus