Skip to content

Commit 08acfad

Browse files
committedOct 28, 2024
Require Node.js 18
1 parent b2e9db2 commit 08acfad

File tree

8 files changed

+28
-25
lines changed

8 files changed

+28
-25
lines changed
 

‎.github/funding.yml

-4
This file was deleted.

‎.github/workflows/main.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
13+
- 22
14+
- 20
15+
- 18
1516
steps:
16-
- uses: actions/checkout@v2
17-
- uses: actions/setup-node@v2
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-node@v4
1819
with:
1920
node-version: ${{ matrix.node-version }}
2021
- run: npm install

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called.
2+
Create a lazy promise that defers execution until it's awaited or when `.then()`, `.catch()`, or `.finally()` is called.
33
*/
44
export default class PLazy<ValueType> extends Promise<ValueType> { // eslint-disable-line @typescript-eslint/naming-convention
55
/**

‎index.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// TODO: Use private class fields when ESLint support it.
2-
31
export default class PLazy extends Promise {
42
#executor;
53
#promise;
@@ -31,18 +29,17 @@ export default class PLazy extends Promise {
3129
}
3230

3331
then(onFulfilled, onRejected) {
34-
// TODO: Use `??=` when targeting Node.js 16.
35-
this.#promise = this.#promise || new Promise(this.#executor);
32+
this.#promise ??= new Promise(this.#executor);
3633
return this.#promise.then(onFulfilled, onRejected);
3734
}
3835

3936
catch(onRejected) {
40-
this.#promise = this.#promise || new Promise(this.#executor);
37+
this.#promise ??= new Promise(this.#executor);
4138
return this.#promise.catch(onRejected);
4239
}
4340

4441
finally(onFinally) {
45-
this.#promise = this.#promise || new Promise(this.#executor);
42+
this.#promise ??= new Promise(this.#executor);
4643
return this.#promise.finally(onFinally);
4744
}
4845
}

‎index.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const lazyPromise = new PLazy<string>((resolve, reject) => {
1010
});
1111

1212
expectType<PLazy<string>>(lazyPromise);
13-
expectType<PLazy<number>>(PLazy.from(async () => Promise.resolve(1)));
13+
expectType<PLazy<number>>(PLazy.from(async () => 1));
1414
expectType<PLazy<number>>(PLazy.from(() => 1));
1515
expectType<PLazy<number>>(PLazy.resolve(1));
1616
expectType<PLazy<never>>(PLazy.reject(new Error('fixture')));

‎package.json

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "p-lazy",
33
"version": "4.0.0",
4-
"description": "Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called",
4+
"description": "Create a lazy promise that defers execution until it's awaited or when `.then()`, `.catch()`, or `.finally()` is called",
55
"license": "MIT",
66
"repository": "sindresorhus/p-lazy",
77
"funding": "https://github.com/sponsors/sindresorhus",
@@ -11,9 +11,13 @@
1111
"url": "https://sindresorhus.com"
1212
},
1313
"type": "module",
14-
"exports": "./index.js",
14+
"exports": {
15+
"types": "./index.d.ts",
16+
"default": "./index.js"
17+
},
18+
"sideEffects": false,
1519
"engines": {
16-
"node": ">=12"
20+
"node": ">=18"
1721
},
1822
"scripts": {
1923
"test": "xo && ava && tsd"
@@ -37,9 +41,14 @@
3741
"bluebird"
3842
],
3943
"devDependencies": {
40-
"ava": "^4.0.0",
41-
"delay": "^5.0.0",
42-
"tsd": "^0.19.1",
43-
"xo": "^0.47.0"
44+
"ava": "^6.2.0",
45+
"tsd": "^0.31.2",
46+
"xo": "^0.59.3"
47+
},
48+
"xo": {
49+
"rules": {
50+
"promise/prefer-await-to-then": "off",
51+
"unicorn/no-thenable": "off"
52+
}
4453
}
4554
}

‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# p-lazy
22

3-
> Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called
3+
> Create a lazy promise that defers execution until it's awaited or when `.then()`, or `.catch()`, or `.finally()` is called
44
55
Useful if you're doing some heavy operations and would like to only do it when the promise is actually used.
66

‎test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import {setTimeout as delay} from 'node:timers/promises';
12
import test from 'ava';
2-
import delay from 'delay';
33
import PLazy from './index.js';
44

55
const fixture = Symbol('fixture');

0 commit comments

Comments
 (0)
Please sign in to comment.