Comparing version 4.0.0 to 5.0.0
/** | ||
Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called. | ||
Create a lazy promise that defers execution until it's awaited or when `.then()`, `.catch()`, or `.finally()` is called. | ||
*/ | ||
export default class PLazy<ValueType> extends Promise<ValueType> { | ||
export default class PLazy<ValueType> extends Promise<ValueType> { // eslint-disable-line @typescript-eslint/naming-convention | ||
/** | ||
@@ -6,0 +6,0 @@ Create a `PLazy` promise from a promise-returning or async function. |
21
index.js
@@ -1,4 +0,5 @@ | ||
// TODO: Use private class fields when ESLint support it. | ||
export default class PLazy extends Promise { | ||
#executor; | ||
#promise; | ||
export default class PLazy extends Promise { | ||
constructor(executor) { | ||
@@ -9,3 +10,3 @@ super(resolve => { | ||
this._executor = executor; | ||
this.#executor = executor; | ||
} | ||
@@ -32,11 +33,15 @@ | ||
then(onFulfilled, onRejected) { | ||
this._promise = this._promise || new Promise(this._executor); | ||
// eslint-disable-next-line promise/prefer-await-to-then | ||
return this._promise.then(onFulfilled, onRejected); | ||
this.#promise ??= new Promise(this.#executor); | ||
return this.#promise.then(onFulfilled, onRejected); | ||
} | ||
catch(onRejected) { | ||
this._promise = this._promise || new Promise(this._executor); | ||
return this._promise.catch(onRejected); | ||
this.#promise ??= new Promise(this.#executor); | ||
return this.#promise.catch(onRejected); | ||
} | ||
finally(onFinally) { | ||
this.#promise ??= new Promise(this.#executor); | ||
return this.#promise.finally(onFinally); | ||
} | ||
} |
{ | ||
"name": "p-lazy", | ||
"version": "4.0.0", | ||
"description": "Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called", | ||
"version": "5.0.0", | ||
"description": "Create a lazy promise that defers execution until it's awaited or when `.then()`, `.catch()`, or `.finally()` is called", | ||
"license": "MIT", | ||
@@ -14,5 +14,9 @@ "repository": "sindresorhus/p-lazy", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"sideEffects": false, | ||
"engines": { | ||
"node": ">=12" | ||
"node": ">=18" | ||
}, | ||
@@ -41,7 +45,12 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^3.15.0", | ||
"delay": "^5.0.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.38.2" | ||
"ava": "^6.2.0", | ||
"tsd": "^0.31.2", | ||
"xo": "^0.59.3" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"promise/prefer-await-to-then": "off", | ||
"unicorn/no-thenable": "off" | ||
} | ||
} | ||
} |
# p-lazy | ||
> Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called | ||
> Create a lazy promise that defers execution until it's awaited or when `.then()`, or `.catch()`, or `.finally()` is called | ||
@@ -9,5 +9,5 @@ Useful if you're doing some heavy operations and would like to only do it when the promise is actually used. | ||
```sh | ||
npm install p-lazy | ||
``` | ||
$ npm install p-lazy | ||
``` | ||
@@ -14,0 +14,0 @@ ## Usage |
5159
3
57