Sha256: 44b91a10c69197d1928db80b5bb89d8b31082efbf6b49caf3a424b6ff85909bf

Contents?: true

Size: 1.21 KB

Versions: 53

Compression:

Stored size: 1.21 KB

Contents

'use strict';
const pLimit = require('p-limit');

class EndError extends Error {
	constructor(value) {
		super();
		this.value = value;
	}
}

// The input can also be a promise, so we await it
const testElement = async (element, tester) => tester(await element);

// The input can also be a promise, so we `Promise.all()` them both
const finder = async element => {
	const values = await Promise.all(element);
	if (values[1] === true) {
		throw new EndError(values[0]);
	}

	return false;
};

const pLocate = async (iterable, tester, options) => {
	options = {
		concurrency: Infinity,
		preserveOrder: true,
		...options
	};

	const limit = pLimit(options.concurrency);

	// Start all the promises concurrently with optional limit
	const items = [...iterable].map(element => [element, limit(testElement, element, tester)]);

	// Check the promises either serially or concurrently
	const checkLimit = pLimit(options.preserveOrder ? 1 : Infinity);

	try {
		await Promise.all(items.map(element => checkLimit(finder, element)));
	} catch (error) {
		if (error instanceof EndError) {
			return error.value;
		}

		throw error;
	}
};

module.exports = pLocate;
// TODO: Remove this for the next major release
module.exports.default = pLocate;

Version data entries

53 entries across 46 versions & 8 rubygems

Version Path
trusty-cms-6.3.1 node_modules/p-locate/index.js
optimacms-0.1.61 spec/dummy/node_modules/@rails/webpacker/node_modules/p-locate/index.js
optimacms-0.1.61 spec/dummy/node_modules/compression-webpack-plugin/node_modules/p-locate/index.js
trusty-cms-5.0.7 node_modules/p-locate/index.js
trusty-cms-5.0.6 node_modules/p-locate/index.js
trusty-cms-5.0.5 node_modules/p-locate/index.js
trusty-cms-5.0.4 node_modules/p-locate/index.js
disco_app-0.16.1 test/dummy/node_modules/find-cache-dir/node_modules/p-locate/index.js
isomorfeus-puppetmaster-0.5.5 node_modules/p-locate/index.js
isomorfeus-puppetmaster-0.5.4 node_modules/p-locate/index.js
disco_app-0.15.2 test/dummy/node_modules/find-cache-dir/node_modules/p-locate/index.js
isomorfeus-puppetmaster-0.5.3 node_modules/p-locate/index.js
trusty-cms-5.0.3 node_modules/p-locate/index.js
trusty-cms-5.0.2 node_modules/p-locate/index.js
disco_app-0.18.4 test/dummy/node_modules/find-cache-dir/node_modules/p-locate/index.js
disco_app-0.18.1 test/dummy/node_modules/find-cache-dir/node_modules/p-locate/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/find-cache-dir/node_modules/p-locate/index.js
disco_app-0.14.0 test/dummy/node_modules/find-cache-dir/node_modules/p-locate/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/find-cache-dir/node_modules/p-locate/index.js
trusty-cms-5.0.1 node_modules/p-locate/index.js