-
Notifications
You must be signed in to change notification settings - Fork 22
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
Showing
3 changed files
with
33 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { expectType } from '../dev' | ||
import { pMap } from './pMap' | ||
|
||
describe('pMap', () => { | ||
test('表现正常', async () => { | ||
const res = await pMap([1, 2], item => | ||
item === 1 ? item : Promise.resolve(item), | ||
) | ||
expectType<typeof res, number[]>() | ||
expect(res).toEqual([1, 2]) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* `Promise.all(data.map(callback))` 的简写。 | ||
* | ||
* @param list 列表数据 | ||
* @param callback 回调 | ||
* @example | ||
* ```typescript | ||
* const res = await pMap( | ||
* [1, 2], | ||
* i => Promise.resolve(i), | ||
* ) | ||
* // => [1, 2] | ||
* ``` | ||
*/ | ||
export function pMap<T, R>( | ||
list: T[], | ||
callback: (item: T, index: number, list: T[]) => R | PromiseLike<R>, | ||
): Promise<R[]> { | ||
return Promise.all(list.map(callback)) | ||
} |