-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --wait option to process URLs sequentially. (#134)
Adds the -w, --wait=<sec> global CLI option to pause between processing URLs for a number of seconds. If unspecified, URLs are processed in parallel as before. Fixes #133.
- Loading branch information
Showing
6 changed files
with
101 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Promise aggregation functions | ||
----------------------------- | ||
*/ | ||
|
||
/* | ||
Run the asynchronous `fn` function sequentially | ||
on each item in the `items` array, with an optional | ||
`delay` in milliseconds between items. | ||
*/ | ||
export function resolveSequence(arr, fn, delay = 0) { | ||
return arr.reduce((chain, item, i) => { | ||
return chain | ||
.then(async result => { | ||
if (delay && i > 0) { | ||
await new Promise(r => setTimeout(r, delay)); | ||
} | ||
return result; | ||
}) | ||
.then(result => { | ||
return fn(item, i, arr).then(content => [...result, content]); | ||
}); | ||
}, Promise.resolve([])); | ||
} | ||
|
||
/* | ||
Run the asynchronous `fn` function in parallel | ||
on each item in the `items` array. | ||
*/ | ||
export function resolveParallel(items, fn) { | ||
return Promise.all(items.map(fn)); | ||
} |
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,31 @@ | ||
import { resolveSequence, resolveParallel } from '../src/util/promises.js'; | ||
import tape from 'tape'; | ||
import { performance } from 'perf_hooks'; | ||
|
||
let arr = [1, 2, 3]; | ||
let epsilon = 100; // milliseconds +/- error | ||
|
||
tape('resolveSequence', async t => { | ||
let begin = performance.now(); | ||
let delay = 500; | ||
let res = await resolveSequence(arr, i => Promise.resolve(i), delay); | ||
let expected_duration = delay * (arr.length - 1); | ||
t.deepEqual(res, arr, 'correct result is returned'); | ||
t.ok( | ||
Math.abs(performance.now() - begin - expected_duration) < epsilon, | ||
'delay is applied' | ||
); | ||
t.end(); | ||
}); | ||
|
||
tape('resolveParallel', async t => { | ||
let begin = performance.now(); | ||
let res = await resolveParallel(arr, i => Promise.resolve(i)); | ||
let expected_duration = 0; | ||
t.deepEqual(res, arr, 'correct result is returned'); | ||
t.ok( | ||
Math.abs(performance.now() - begin - expected_duration) < epsilon, | ||
"there's no delay" | ||
); | ||
t.end(); | ||
}); |