Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 6, 2021
1 parent 6b338f1 commit 3a103c3
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 84 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
76 changes: 29 additions & 47 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,40 @@
declare namespace pWaitFor {
interface Options {
/**
Number of milliseconds to wait before retrying `condition`.
export interface Options {
/**
Number of milliseconds to wait before retrying `condition`.
@default 20
*/
readonly interval?: number;
@default 20
*/
readonly interval?: number;

/**
Number of milliseconds to wait before automatically rejecting.
/**
Number of milliseconds to wait before automatically rejecting.
@default Infinity
*/
readonly timeout?: number;
@default Infinity
*/
readonly timeout?: number;

/**
Whether to run the check immediately rather than starting by waiting `interval` milliseconds.
/**
Whether to run the check immediately rather than starting by waiting `interval` milliseconds.
Useful for when the check, if run immediately, would likely return `false`. In this scenario, set `before` to `false`.
Useful for when the check, if run immediately, would likely return `false`. In this scenario, set `before` to `false`.
@default true
*/
readonly before?: boolean;
}
@default true
*/
readonly before?: boolean;
}

declare const pWaitFor: {
/**
Wait for a condition to be true.
/**
Wait for a condition to be true.
@returns A promise that resolves when `condition` returns `true`. Rejects if `condition` throws or returns a `Promise` that rejects.
@returns A promise that resolves when `condition` returns `true`. Rejects if `condition` throws or returns a `Promise` that rejects.
@example
```
import pWaitFor = require('p-wait-for');
import pathExists = require('path-exists');
@example
```
import pWaitFor from 'p-wait-for';
import pathExists from 'path-exists';
(async () => {
await pWaitFor(() => pathExists('unicorn.png'));
console.log('Yay! The file now exists.');
})();
```
*/
(condition: () => PromiseLike<boolean> | boolean, options?: pWaitFor.Options): Promise<
void
>;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pWaitFor(
// condition: () => PromiseLike<boolean> | boolean,
// options?: pWaitFor.Options
// ): Promise<void>;
// export = pWaitFor;
default: typeof pWaitFor;
};

export = pWaitFor;
await pWaitFor(() => pathExists('unicorn.png'));
console.log('Yay! The file now exists.');
```
*/
export default function pWaitFor(condition: () => PromiseLike<boolean> | boolean, options?: Options): Promise<void>;
32 changes: 13 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';
const pTimeout = require('p-timeout');
import pTimeout from 'p-timeout';

const pWaitFor = async (condition, options) => {
options = {
interval: 20,
timeout: Infinity,
before: true,
...options
};
export default async function pWaitFor(condition, options = {}) {
const {
interval = 20,
timeout = Number.POSITIVE_INFINITY,
before = true
} = options;

let retryTimeout;

Expand All @@ -23,23 +21,23 @@ const pWaitFor = async (condition, options) => {
if (value === true) {
resolve();
} else {
retryTimeout = setTimeout(check, options.interval);
retryTimeout = setTimeout(check, interval);
}
} catch (error) {
reject(error);
}
};

if (options.before) {
if (before) {
check();
} else {
retryTimeout = setTimeout(check, options.interval);
retryTimeout = setTimeout(check, interval);
}
});

if (options.timeout !== Infinity) {
if (timeout !== Number.POSITIVE_INFINITY) {
try {
return await pTimeout(promise, options.timeout);
return await pTimeout(promise, timeout);
} catch (error) {
if (retryTimeout) {
clearTimeout(retryTimeout);
Expand All @@ -50,8 +48,4 @@ const pWaitFor = async (condition, options) => {
}

return promise;
};

module.exports = pWaitFor;
// TODO: Remove this for the next major release
module.exports.default = pWaitFor;
}
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {expectType} from 'tsd';
import pWaitFor = require('.');
import pWaitFor from './index.js';

expectType<Promise<void>>(pWaitFor(() => false));
expectType<Promise<void>>(pWaitFor(() => Promise.resolve(false)));
expectType<Promise<void>>(pWaitFor(async () => false));
expectType<Promise<void>>(pWaitFor(() => true, {interval: 1}));
expectType<Promise<void>>(pWaitFor(() => true, {timeout: 1}));
expectType<Promise<void>>(pWaitFor(() => true, {before: false}));
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -35,13 +37,13 @@
"bluebird"
],
"dependencies": {
"p-timeout": "^3.0.0"
"p-timeout": "^5.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.1.0",
"time-span": "^3.0.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"delay": "^5.0.0",
"time-span": "^4.0.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
10 changes: 4 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ $ npm install p-wait-for
## Usage

```js
const pWaitFor = require('p-wait-for');
const pathExists = require('path-exists');
import pWaitFor from 'p-wait-for';
import pathExists from 'path-exists';

(async () => {
await pWaitFor(() => pathExists('unicorn.png'));
console.log('Yay! The file now exists.');
})();
await pWaitFor(() => pathExists('unicorn.png'));
console.log('Yay! The file now exists.');
```

## API
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import delay from 'delay';
import timeSpan from 'time-span';
import pWaitFor from '.';
import pWaitFor from './index.js';

test('waits for condition', async t => {
const ms = 200;
Expand Down

0 comments on commit 3a103c3

Please sign in to comment.