Skip to content

Commit 5b0780c

Browse files
committedApr 9, 2021
Require Node.js 12 and move to ESM
1 parent b256354 commit 5b0780c

File tree

8 files changed

+34
-51
lines changed

8 files changed

+34
-51
lines changed
 

‎.github/workflows/main.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ jobs:
1212
node-version:
1313
- 14
1414
- 12
15-
- 10
16-
- 8
1715
steps:
1816
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
17+
- uses: actions/setup-node@v2
2018
with:
2119
node-version: ${{ matrix.node-version }}
2220
- run: npm install

‎index.d.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
/**
22
Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called.
33
*/
4-
declare class PLazy<ValueType> extends Promise<ValueType> {
4+
export default class PLazy<ValueType> extends Promise<ValueType> {
55
/**
66
Create a `PLazy` promise from a promise-returning or async function.
77
88
@example
99
```
10-
import PLazy = require('p-lazy');
10+
import PLazy from 'p-lazy';
1111
1212
const lazyPromise = new PLazy(resolve => {
1313
someHeavyOperation(resolve);
1414
});
1515
1616
// `someHeavyOperation` is not yet called
1717
18-
(async () => {
19-
await doSomethingFun;
20-
// `someHeavyOperation` is called
21-
console.log(await lazyPromise);
22-
})();
18+
await doSomethingFun;
19+
20+
// `someHeavyOperation` is called
21+
console.log(await lazyPromise);
2322
```
2423
*/
25-
static from<ValueType>(
26-
fn: () => ValueType | PromiseLike<ValueType>
27-
): PLazy<ValueType>;
24+
static from<ValueType>(function_: () => ValueType | PromiseLike<ValueType>): PLazy<ValueType>;
2825
}
29-
30-
export = PLazy;

‎index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
'use strict';
1+
// TODO: Use private class fields when ESLint support it.
22

3-
class PLazy extends Promise {
3+
export default class PLazy extends Promise {
44
constructor(executor) {
55
super(resolve => {
66
resolve();
@@ -9,9 +9,9 @@ class PLazy extends Promise {
99
this._executor = executor;
1010
}
1111

12-
static from(fn) {
12+
static from(function_) {
1313
return new PLazy(resolve => {
14-
resolve(fn());
14+
resolve(function_());
1515
});
1616
}
1717

@@ -38,5 +38,3 @@ class PLazy extends Promise {
3838
return this._promise.catch(onRejected);
3939
}
4040
}
41-
42-
module.exports = PLazy;

‎index.test-d.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import {expectType} from 'tsd';
2-
import PLazy = require('.');
2+
import PLazy from './index.js';
33

44
const lazyPromise = new PLazy<string>((resolve, reject) => {
5-
if (!true) {
6-
resolve('foo');
5+
if (true) {
6+
reject(new Error('fixture'));
77
} else {
8-
reject(new Error());
8+
resolve('foo');
99
}
1010
});
1111

1212
expectType<PLazy<string>>(lazyPromise);
13-
14-
expectType<PLazy<number>>(PLazy.from(() => Promise.resolve(1)));
13+
expectType<PLazy<number>>(PLazy.from(async () => Promise.resolve(1)));
1514
expectType<PLazy<number>>(PLazy.from(() => 1));
1615
expectType<PLazy<number>>(PLazy.resolve(1));
17-
expectType<PLazy<never>>(PLazy.reject(new Error()));
16+
expectType<PLazy<never>>(PLazy.reject(new Error('fixture')));

‎license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

‎package.json

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Create a lazy promise that defers execution until it's awaited or when `.then()` or `.catch()` is called",
55
"license": "MIT",
66
"repository": "sindresorhus/p-lazy",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": ">=12"
1417
},
1518
"scripts": {
1619
"test": "xo && ava && tsd"
@@ -34,9 +37,9 @@
3437
"bluebird"
3538
],
3639
"devDependencies": {
37-
"ava": "^1.4.1",
38-
"delay": "^4.1.0",
39-
"tsd": "^0.7.2",
40-
"xo": "^0.24.0"
40+
"ava": "^3.15.0",
41+
"delay": "^5.0.0",
42+
"tsd": "^0.14.0",
43+
"xo": "^0.38.2"
4144
}
4245
}

‎readme.md

+5-15
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,28 @@
44
55
Useful if you're doing some heavy operations and would like to only do it when the promise is actually used.
66

7-
87
## Install
98

109
```
1110
$ npm install p-lazy
1211
```
1312

14-
1513
## Usage
1614

1715
```js
18-
const PLazy = require('p-lazy');
16+
import PLazy from 'p-lazy';
1917

2018
const lazyPromise = new PLazy(resolve => {
2119
someHeavyOperation(resolve);
2220
});
2321

2422
// `someHeavyOperation` is not yet called
2523

26-
(async () => {
27-
await doSomethingFun;
28-
// `someHeavyOperation` is called
29-
console.log(await lazyPromise);
30-
})();
31-
```
24+
await doSomethingFun;
3225

26+
// `someHeavyOperation` is called
27+
console.log(await lazyPromise);
28+
```
3329

3430
## API
3531

@@ -49,16 +45,10 @@ Create a `PLazy` promise that is resolved with the given `value`, or the promise
4945

5046
Create a `PLazy` promise that is rejected with the given `reason`.
5147

52-
5348
## Related
5449

5550
- [p-cancelable](https://github.com/sindresorhus/p-cancelable) - Create a promise that can be canceled
5651
- [p-defer](https://github.com/sindresorhus/p-defer) - Create a deferred promise
5752
- [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value
5853
- [define-lazy-prop](https://github.com/sindresorhus/define-lazy-prop) - Define a lazily evaluated property on an object
5954
- [More…](https://github.com/sindresorhus/promise-fun)
60-
61-
62-
## License
63-
64-
MIT © [Sindre Sorhus](https://sindresorhus.com)

‎test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava';
22
import delay from 'delay';
3-
import PLazy from '.';
3+
import PLazy from './index.js';
44

55
const fixture = Symbol('fixture');
66

0 commit comments

Comments
 (0)
Please sign in to comment.