Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55d25f6
commit fa505fe
Showing
8 changed files
with
44 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters