Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/p-lazy
Create a lazy promise that defers execution until it's awaited or when .then() or .catch() is called
https://github.com/sindresorhus/p-lazy
Last synced: about 1 month ago
JSON representation
Create a lazy promise that defers execution until it's awaited or when .then() or .catch() is called
- Host: GitHub
- URL: https://github.com/sindresorhus/p-lazy
- Owner: sindresorhus
- License: mit
- Created: 2016-11-07T09:05:39.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2022-07-09T03:22:46.000Z (over 2 years ago)
- Last Synced: 2024-04-14T09:52:40.176Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 17.6 KB
- Stars: 263
- Watchers: 8
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- promise-fun - p-lazy
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
```sh
npm install p-lazy
```## Usage
```js
import PLazy from 'p-lazy';const lazyPromise = new PLazy(resolve => {
someHeavyOperation(resolve);
});// `someHeavyOperation` is not yet called
await doSomethingFun;
// `someHeavyOperation` is called
console.log(await lazyPromise);
```## API
### new PLazy(executor)
Same as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise). `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](https://github.com/sindresorhus/p-cancelable) - Create a promise that can be canceled
- [p-defer](https://github.com/sindresorhus/p-defer) - Create a deferred promise
- [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value
- [define-lazy-prop](https://github.com/sindresorhus/define-lazy-prop) - Define a lazily evaluated property on an object
- [More…](https://github.com/sindresorhus/promise-fun)