-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Open fewer files at once during migration.
- Loading branch information
1 parent
40af747
commit f9eb590
Showing
5 changed files
with
84 additions
and
3 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,64 @@ | ||
const { asyncSeriesMap } = require('./asyncSeriesMap') | ||
describe('asyncSerialMap', () => { | ||
const nextTick = ms => new Promise((resolve) => { | ||
process.nextTick(() => resolve()) | ||
}) | ||
|
||
it('should work through an array and return the results', async () => { | ||
const arr = ['abc', 'def', 'ghi'] | ||
const handler = async (item) => `"${item}"` | ||
|
||
const result = await asyncSeriesMap(arr, handler) | ||
|
||
expect(result).toEqual([ | ||
'"abc"', | ||
'"def"', | ||
'"ghi"' | ||
]) | ||
}) | ||
|
||
it('should work through an array and return the results', async () => { | ||
const arr = ['abc', 'def', 'ghi'] | ||
const handler = async (item, index, array) => `"${item}|${index}|${JSON.stringify(array)}"` | ||
|
||
const result = await asyncSeriesMap(arr, handler) | ||
|
||
expect(result).toEqual([ | ||
'"abc|0|["abc","def","ghi"]"', | ||
'"def|1|["abc","def","ghi"]"', | ||
'"ghi|2|["abc","def","ghi"]"' | ||
]) | ||
}) | ||
|
||
it('should run these in series', async () => { | ||
let latestResolve | ||
let callCount = 0 | ||
const handler = (item) => new Promise((resolve, reject) => { | ||
callCount++ | ||
latestResolve = resolve | ||
}) | ||
|
||
const resultPromise = asyncSeriesMap(['abc', 'def', 'ghi'], handler) | ||
|
||
await nextTick() | ||
|
||
expect(callCount).toBe(1) | ||
latestResolve('this is the first') | ||
|
||
await nextTick() | ||
|
||
expect(callCount).toBe(2) | ||
latestResolve('this is the second') | ||
|
||
await nextTick() | ||
|
||
expect(callCount).toBe(3) | ||
latestResolve('this is the third') | ||
|
||
expect(await resultPromise).toEqual([ | ||
'this is the first', | ||
'this is the second', | ||
'this is the third' | ||
]) | ||
}) | ||
}) |
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 @@ | ||
async function asyncSeriesMap (arr, handler) { | ||
const results = [] | ||
let index = -1 | ||
while (arr.length > ++index) { | ||
results.push(await handler(arr[index], index, arr)) | ||
} | ||
return results | ||
} | ||
|
||
module.exports = { | ||
asyncSeriesMap | ||
} |
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 |
---|---|---|
|
@@ -126,6 +126,6 @@ | |
"/node_modules/", | ||
"/tmp/" | ||
], | ||
"testTimeout": 30000 | ||
"testTimeout": 5000 | ||
} | ||
} |