-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use promise.all in asyncForEach if wanted
There is a warning if you use `inSequence`, which is the default. In the future the promise.all method will be the default.
- Loading branch information
Showing
3 changed files
with
51 additions
and
7 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,12 +1,40 @@ | ||
import {defaults, DeepPartial} from './defaults' | ||
|
||
interface AsyncForEachOptions{ | ||
/** Use the legacy style for look for itterations, when false this is a wrapper for `Promise.all` | ||
* The default in 1.x will be to set this to `false`. For 0.x it is `true` | ||
*/ | ||
inSequence: boolean | ||
} | ||
|
||
const defaultOptions: AsyncForEachOptions = { | ||
inSequence: true | ||
} | ||
|
||
/** | ||
* Runs the supplied itterator for all elements in the array asyncronously. | ||
* | ||
* @param array The array to itterate through. | ||
* @param itterator The async function to run for each element. | ||
*/ | ||
export const asyncForEach = async <T>(array: T[], itterator: (value: T, index: number, array: T[]) => Promise<void>): Promise<void> => { | ||
for(let index =0; index < array.length; index++){ | ||
//eslint-disable-next-line | ||
await itterator(array[index], index, array) | ||
export const asyncForEach = async <T>(array: T[], itterator: (value: T, index: number, array: T[]) => Promise<void>, options?: DeepPartial<AsyncForEachOptions>): Promise<void> => { | ||
const {inSequence} = defaults(options, defaultOptions) | ||
|
||
if(inSequence){ | ||
console.warn('in sequence is going to be removed in the future, for 0.x it is default on, soon it will change to default off.') | ||
for(let index =0; index < array.length; index++){ | ||
//eslint-disable-next-line | ||
await itterator(array[index], index, array) | ||
} | ||
|
||
return | ||
} | ||
|
||
const promises: Promise<void>[] = [] | ||
|
||
array.forEach((value, index, arr) => { | ||
promises.push(itterator(value, index, arr)) | ||
}) | ||
|
||
await Promise.all(promises) | ||
} |
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