# p-lazy

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

Latest version **5.0.0** (published 2024-10-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-lazy
pnpm add p-lazy
yarn add p-lazy
bun add p-lazy
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2024-10-28 |
| First published | 2016-11-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 285 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, lazy, defer, deferred, then, catch, fulfilled, async, function, await, promises, bluebird |

## Links

- npm: https://www.npmjs.com/package/p-lazy
- Repository: https://github.com/sindresorhus/p-lazy
- Homepage: https://github.com/sindresorhus/p-lazy#readme
- Issues: https://github.com/sindresorhus/p-lazy/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/p-lazy

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 5.0.0 (latest) — 2024-10-28
- 4.0.0 — 2021-04-09
- 3.1.0 — 2021-01-10
- 3.0.0 — 2019-04-03
- 2.0.0 — 2019-03-16
- 1.0.0 — 2016-11-07

## README

# p-lazy

> Create a lazy promise that defers execution until it's awaited or when `.then()`, or `.catch()`, or `.finally()` 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)

---
_Source: https://npm.io/package/p-lazy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
