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 9, 2021
1 parent 55d25f6 commit fa505fe
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 75 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Expand Up @@ -12,11 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
54 changes: 20 additions & 34 deletions index.d.ts
@@ -1,39 +1,25 @@
declare const pWhilst: {
/**
While `condition` returns `true`, executes `action` repeatedly, and then resolves the promise. Rejects if `action` returns a promise that rejects or if an error is thrown anywhere.
/**
While `condition` returns `true`, executes `action` repeatedly, and then resolves the promise. Rejects if `action` returns a promise that rejects or if an error is thrown anywhere.
@param condition - Expected to return a boolean of whether to execute `action`.
@param action - Action to run for each iteration. You can return a promise and it will be handled.
@param condition - Expected to return a boolean of whether to execute `action`.
@param action - Action to run for each iteration. You can return a promise and it will be handled.
@example
```
import pWhilst = require('p-whilst');
@example
```
import pWhilst from 'p-whilst';
(async () => {
let count = 0;
let count = 0;
await pWhilst(
() => count < 5,
() => count++
);
await pWhilst(
() => count < 5,
() => count++
);
console.log(count);
//=> 5
})();
```
*/
<ValueType>(
condition: (value: ValueType | undefined) => boolean,
action: () => ValueType | PromiseLike<ValueType>
): Promise<void>;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pWhilst<ValueType>(
// condition: (value: ValueType | undefined) => boolean,
// action: () => ValueType | PromiseLike<ValueType>
// ): Promise<void>;
// export = pWhilst;
default: typeof pWhilst;
};

export = pWhilst;
console.log(count);
//=> 5
```
*/
export default function pWhilst<ValueType>(
condition: (value: ValueType | undefined) => boolean,
action: () => ValueType | PromiseLike<ValueType>
): Promise<void>;
10 changes: 2 additions & 8 deletions index.js
@@ -1,15 +1,9 @@
'use strict';

const pWhilst = async (condition, action) => {
export default async function pWhilst(condition, action) {
const loop = async actionResult => {
if (condition(actionResult)) {
return loop(await action());
}
};

return loop();
};

module.exports = pWhilst;
// TODO: Remove this for the next major release
module.exports.default = pWhilst;
}
3 changes: 1 addition & 2 deletions index.test-d.ts
@@ -1,13 +1,12 @@
import {expectType} from 'tsd';
import pWhilst = require('.');
import pWhilst from './index.js';

let count = 0;

expectType<Promise<void>>(
pWhilst(
currentCount => {
expectType<number | undefined>(currentCount);

return currentCount === undefined ? true : currentCount < 5;
},
() => count++
Expand Down
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

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

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:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Expand Up @@ -4,13 +4,16 @@
"description": "While a condition returns true, calls a function repeatedly, and then resolves the promise",
"license": "MIT",
"repository": "sindresorhus/p-whilst",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -33,8 +36,8 @@
"bluebird"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
29 changes: 9 additions & 20 deletions readme.md
Expand Up @@ -4,33 +4,28 @@
Think async version of the [`while` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while).


## Install

```
$ npm install p-whilst
```


## Usage

```js
const pWhilst = require('p-whilst');
import pWhilst from 'p-whilst';

(async () => {
let count = 0;
let count = 0;

await pWhilst(
() => count < 5,
() => count++
);
await pWhilst(
() => count < 5,
() => count++
);

console.log(count);
//=> 5
})();
console.log(count);
//=> 5
```


## API

### pWhilst(condition, action)
Expand All @@ -39,7 +34,7 @@ While `condition` returns `true`, executes `action` repeatedly, and then resolve

#### condition

Type: `Function`<br>
Type: `Function`\
Arguments: The value the `action` function returns

Expected to return a boolean of whether to execute `action`.
Expand All @@ -52,15 +47,9 @@ Action to run for each iteration.

You can return a promise and it will be handled.


## Related

- [p-do-whilst](https://github.com/sindresorhus/p-do-whilst) - Calls a function repeatedly while a condition returns true and then resolves the promise
- [p-forever](https://github.com/sindresorhus/p-forever) - Run promise-returning & async functions repeatedly until you end it
- [p-wait-for](https://github.com/sindresorhus/p-wait-for) - Wait for a condition to be true
- [More…](https://github.com/sindresorhus/promise-fun)


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
4 changes: 2 additions & 2 deletions test.js
@@ -1,5 +1,5 @@
import test from 'ava';
import pWhilst from '.';
import pWhilst from './index.js';

test('main', async t => {
const result = [];
Expand Down Expand Up @@ -87,7 +87,7 @@ test('stops on error', async t => {
})
);

await t.throwsAsync(promise, 'BAAD');
await t.throwsAsync(promise, {message: 'BAAD'});
t.is(counter, 7);
t.deepEqual(result, [0, 1, 2, 3, 4, 5, 6]);
});

0 comments on commit fa505fe

Please sign in to comment.