p-lazy
Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called
Last updated 6 years ago by sindresorhus .
MIT · Repository · Bugs · Original npm
$ cnpm install p-lazy 
SYNC missed versions from official npm registry.

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

Current Tags

  • 3.0.0                                ...           latest (6 years ago)

2 Versions

  • 3.0.0                                ...           6 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (4)
Dependents (0)
None

Copyright 2013 - present © cnpmjs.org