Skip to content

Commit

Permalink
Require Node.js 10
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
sindresorhus committed Aug 10, 2020
1 parent b16a5b1 commit 7bd05da
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '14'
- '12'
- '10'
- '8'
73 changes: 31 additions & 42 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,36 @@ declare namespace pLocate {
}
}

declare const pLocate: {
/**
Get the first fulfilled promise that satisfies the provided testing function.
@param input - An iterable of promises/values to test.
@param tester - This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
@returns A `Promise` that is fulfilled when `tester` resolves to `true` or the iterable is done, or rejects if any of the promises reject. The fulfilled value is the current iterable value or `undefined` if `tester` never resolved to `true`.
@example
```
import pathExists = require('path-exists');
import pLocate = require('p-locate');
const files = [
'unicorn.png',
'rainbow.png', // Only this one actually exists on disk
'pony.png'
];
(async () => {
const foundPath = await pLocate(files, file => pathExists(file));
console.log(foundPath);
//=> 'rainbow'
})();
```
*/
<ValueType>(
input: Iterable<PromiseLike<ValueType> | ValueType>,
tester: (element: ValueType) => PromiseLike<boolean> | boolean,
options?: pLocate.Options
): Promise<ValueType | undefined>;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pLocate<ValueType>(
// input: Iterable<PromiseLike<ValueType> | ValueType>,
// tester: (element: ValueType) => PromiseLike<boolean> | boolean,
// options?: pLocate.Options
// ): Promise<ValueType | undefined>;
// export = pLocate;
default: typeof pLocate;
};
/**
Get the first fulfilled promise that satisfies the provided testing function.
@param input - An iterable of promises/values to test.
@param tester - This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
@returns A `Promise` that is fulfilled when `tester` resolves to `true` or the iterable is done, or rejects if any of the promises reject. The fulfilled value is the current iterable value or `undefined` if `tester` never resolved to `true`.
@example
```
import pathExists = require('path-exists');
import pLocate = require('p-locate');
const files = [
'unicorn.png',
'rainbow.png', // Only this one actually exists on disk
'pony.png'
];
(async () => {
const foundPath = await pLocate(files, file => pathExists(file));
console.log(foundPath);
//=> 'rainbow'
})();
```
*/
declare function pLocate<ValueType>(
input: Iterable<PromiseLike<ValueType> | ValueType>,
tester: (element: ValueType) => PromiseLike<boolean> | boolean,
options?: pLocate.Options
): Promise<ValueType | undefined>;

export = pLocate;
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,3 @@ const pLocate = async (iterable, tester, options) => {
};

module.exports = pLocate;
// TODO: Remove this for the next major release
module.exports.default = pLocate;
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"description": "Get the first fulfilled promise that satisfies the provided testing function",
"license": "MIT",
"repository": "sindresorhus/p-locate",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -40,14 +41,14 @@
"bluebird"
],
"dependencies": {
"p-limit": "^2.2.0"
"p-limit": "^3.0.2"
},
"devDependencies": {
"ava": "^1.4.1",
"ava": "^2.4.0",
"delay": "^4.1.0",
"in-range": "^1.0.0",
"time-span": "^3.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"in-range": "^2.0.0",
"time-span": "^4.0.0",
"tsd": "^0.13.1",
"xo": "^0.32.1"
}
}
13 changes: 4 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# p-locate [![Build Status](https://travis-ci.org/sindresorhus/p-locate.svg?branch=master)](https://travis-ci.org/sindresorhus/p-locate)
# p-locate [![Build Status](https://travis-ci.com/sindresorhus/p-locate.svg?branch=master)](https://travis-ci.com/github/sindresorhus/p-locate)

> Get the first fulfilled promise that satisfies the provided testing function
Think of it like an async version of [`Array#find`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find).


## Install

```
$ npm install p-locate
```


## Usage

Here we find the first file that exists on disk, in array order.
Expand All @@ -36,7 +34,6 @@ const files = [

*The above is just an example. Use [`locate-path`](https://github.com/sindresorhus/locate-path) if you need this.*


## API

### pLocate(input, tester, options?)
Expand All @@ -61,30 +58,28 @@ Type: `object`

##### concurrency

Type: `number`<br>
Default: `Infinity`<br>
Type: `number`\
Default: `Infinity`\
Minimum: `1`

Number of concurrently pending promises returned by `tester`.

##### preserveOrder

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Preserve `input` order when searching.

Disable this to improve performance if you don't care about the order.


## Related

- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
- [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently
- [p-any](https://github.com/sindresorhus/p-any) - Wait for any promise to be fulfilled
- [More…](https://github.com/sindresorhus/promise-fun)


---

<div align="center">
Expand Down
12 changes: 8 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ const tester = async ([value, ms]) => {
test('main', async t => {
const end = timeSpan();
t.is((await pLocate(input, tester))[0], 2);
t.true(inRange(end(), 370, 450), 'should be time of item `2`');
t.true(inRange(end(), {start: 370, end: 450}), 'should be time of item `2`');
});

test('option {preserveOrder:false}', async t => {
const end = timeSpan();
t.is((await pLocate(input, tester, {preserveOrder: false}))[0], 3);
t.true(inRange(end(), 170, 250), 'should be time of item `3`');
t.true(inRange(end(), {start: 170, end: 250}), 'should be time of item `3`');
});

test('option {concurrency:1}', async t => {
const end = timeSpan();
t.is((await pLocate(input, tester, {concurrency: 1}))[0], 2);
t.true(inRange(end(), 670, 750), 'should be time of items `1` and `2`, since they run serially');
t.true(inRange(end(), {start: 670, end: 750}), 'should be time of items `1` and `2`, since they run serially');
});

test('returns `undefined` when nothing could be found', async t => {
Expand All @@ -40,5 +40,9 @@ test('returns `undefined` when nothing could be found', async t => {

test('rejected return value in `tester` rejects the promise', async t => {
const fixtureError = new Error('fixture');
await t.throwsAsync(pLocate([1, 2, 3], () => Promise.reject(fixtureError)), fixtureError.message);

await t.throwsAsync(
pLocate([1, 2, 3], () => Promise.reject(fixtureError)),
fixtureError.message
);
});

0 comments on commit 7bd05da

Please sign in to comment.