p-locate
Advanced tools
Comparing version 5.0.0 to 6.0.0
@@ -1,19 +0,19 @@ | ||
declare namespace pLocate { | ||
interface Options { | ||
/** | ||
Number of concurrently pending promises returned by `tester`. Minimum: `1`. | ||
export interface Options { | ||
/** | ||
The number of concurrently pending promises returned by `tester`. | ||
@default Infinity | ||
*/ | ||
readonly concurrency?: number; | ||
Minimum: `1` | ||
/** | ||
Preserve `input` order when searching. | ||
@default Infinity | ||
*/ | ||
readonly concurrency?: number; | ||
Disable this to improve performance if you don't care about the order. | ||
/** | ||
Preserve `input` order when searching. | ||
@default true | ||
*/ | ||
readonly preserveOrder?: boolean; | ||
} | ||
Disable this to improve performance if you don't care about the order. | ||
@default true | ||
*/ | ||
readonly preserveOrder?: boolean; | ||
} | ||
@@ -30,4 +30,4 @@ | ||
``` | ||
import pathExists = require('path-exists'); | ||
import pLocate = require('p-locate'); | ||
import {pathExists} from 'path-exists'; | ||
import pLocate from 'p-locate'; | ||
@@ -40,16 +40,12 @@ const files = [ | ||
(async () => { | ||
const foundPath = await pLocate(files, file => pathExists(file)); | ||
const foundPath = await pLocate(files, file => pathExists(file)); | ||
console.log(foundPath); | ||
//=> 'rainbow' | ||
})(); | ||
console.log(foundPath); | ||
//=> 'rainbow' | ||
``` | ||
*/ | ||
declare function pLocate<ValueType>( | ||
export default function pLocate<ValueType>( | ||
input: Iterable<PromiseLike<ValueType> | ValueType>, | ||
tester: (element: ValueType) => PromiseLike<boolean> | boolean, | ||
options?: pLocate.Options | ||
options?: Options | ||
): Promise<ValueType | undefined>; | ||
export = pLocate; |
34
index.js
@@ -1,3 +0,2 @@ | ||
'use strict'; | ||
const pLimit = require('p-limit'); | ||
import pLimit from 'p-limit'; | ||
@@ -11,6 +10,6 @@ class EndError extends Error { | ||
// The input can also be a promise, so we await it | ||
// 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 | ||
// The input can also be a promise, so we `Promise.all()` them both. | ||
const finder = async element => { | ||
@@ -25,16 +24,17 @@ const values = await Promise.all(element); | ||
const pLocate = async (iterable, tester, options) => { | ||
options = { | ||
concurrency: Infinity, | ||
preserveOrder: true, | ||
...options | ||
}; | ||
export default async function pLocate( | ||
iterable, | ||
tester, | ||
{ | ||
concurrency = Number.POSITIVE_INFINITY, | ||
preserveOrder = true, | ||
} = {}, | ||
) { | ||
const limit = pLimit(concurrency); | ||
const limit = pLimit(options.concurrency); | ||
// Start all the promises concurrently with optional limit | ||
// 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); | ||
// Check the promises either serially or concurrently. | ||
const checkLimit = pLimit(preserveOrder ? 1 : Number.POSITIVE_INFINITY); | ||
@@ -50,4 +50,2 @@ try { | ||
} | ||
}; | ||
module.exports = pLocate; | ||
} |
{ | ||
"name": "p-locate", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Get the first fulfilled promise that satisfies the provided testing function", | ||
@@ -13,4 +13,6 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -45,12 +47,12 @@ "scripts": { | ||
"dependencies": { | ||
"p-limit": "^3.0.2" | ||
"p-limit": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"delay": "^4.1.0", | ||
"in-range": "^2.0.0", | ||
"time-span": "^4.0.0", | ||
"tsd": "^0.13.1", | ||
"xo": "^0.32.1" | ||
"ava": "^3.15.0", | ||
"delay": "^5.0.0", | ||
"in-range": "^3.0.0", | ||
"time-span": "^5.0.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# p-locate [![Build Status](https://travis-ci.com/sindresorhus/p-locate.svg?branch=master)](https://travis-ci.com/github/sindresorhus/p-locate) | ||
# p-locate | ||
@@ -18,4 +18,4 @@ > Get the first fulfilled promise that satisfies the provided testing function | ||
```js | ||
const pathExists = require('path-exists'); | ||
const pLocate = require('p-locate'); | ||
import {pathExists} from 'path-exists'; | ||
import pLocate from 'p-locate'; | ||
@@ -28,8 +28,6 @@ const files = [ | ||
(async () => { | ||
const foundPath = await pLocate(files, file => pathExists(file)); | ||
const foundPath = await pLocate(files, file => pathExists(file)); | ||
console.log(foundPath); | ||
//=> 'rainbow' | ||
})(); | ||
console.log(foundPath); | ||
//=> 'rainbow' | ||
``` | ||
@@ -67,3 +65,3 @@ | ||
Number of concurrently pending promises returned by `tester`. | ||
The number of concurrently pending promises returned by `tester`. | ||
@@ -70,0 +68,0 @@ ##### preserveOrder |
Yes
7032
77
92
+ Addedp-limit@4.0.0(transitive)
+ Addedyocto-queue@1.0.0(transitive)
- Removedp-limit@3.1.0(transitive)
- Removedyocto-queue@0.1.0(transitive)
Updatedp-limit@^4.0.0