Last change
on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 | const pLimit = require('p-limit');
|
---|
| 3 |
|
---|
| 4 | class EndError extends Error {
|
---|
| 5 | constructor(value) {
|
---|
| 6 | super();
|
---|
| 7 | this.value = value;
|
---|
| 8 | }
|
---|
| 9 | }
|
---|
| 10 |
|
---|
| 11 | // The input can also be a promise, so we await it
|
---|
| 12 | const testElement = async (element, tester) => tester(await element);
|
---|
| 13 |
|
---|
| 14 | // The input can also be a promise, so we `Promise.all()` them both
|
---|
| 15 | const finder = async element => {
|
---|
| 16 | const values = await Promise.all(element);
|
---|
| 17 | if (values[1] === true) {
|
---|
| 18 | throw new EndError(values[0]);
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | return false;
|
---|
| 22 | };
|
---|
| 23 |
|
---|
| 24 | const pLocate = async (iterable, tester, options) => {
|
---|
| 25 | options = {
|
---|
| 26 | concurrency: Infinity,
|
---|
| 27 | preserveOrder: true,
|
---|
| 28 | ...options
|
---|
| 29 | };
|
---|
| 30 |
|
---|
| 31 | const limit = pLimit(options.concurrency);
|
---|
| 32 |
|
---|
| 33 | // Start all the promises concurrently with optional limit
|
---|
| 34 | const items = [...iterable].map(element => [element, limit(testElement, element, tester)]);
|
---|
| 35 |
|
---|
| 36 | // Check the promises either serially or concurrently
|
---|
| 37 | const checkLimit = pLimit(options.preserveOrder ? 1 : Infinity);
|
---|
| 38 |
|
---|
| 39 | try {
|
---|
| 40 | await Promise.all(items.map(element => checkLimit(finder, element)));
|
---|
| 41 | } catch (error) {
|
---|
| 42 | if (error instanceof EndError) {
|
---|
| 43 | return error.value;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | throw error;
|
---|
| 47 | }
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | module.exports = pLocate;
|
---|
| 51 | // TODO: Remove this for the next major release
|
---|
| 52 | module.exports.default = pLocate;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.