p-locate

中文文档
7.0.0发布于 2026/02/03MITTypeScriptES Module近 30 天下载 12.6亿

Get the first fulfilled promise that satisfies the provided testing function

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. API
  4. pLocate(input, tester, options?)
  5. Related

p-locate

Get the first fulfilled promise that satisfies the provided testing function

Think of it like an async version of Array#find.

Install

npm install p-locate

Usage

Here we find the first file that exists on disk, in array order.

import {pathExists} from 'path-exists';
import pLocate from 'p-locate';

const files = [
    'unicorn.png',
    'rainbow.png', // Only this one actually exists on disk
    'pony.png'
];

const foundPath = await pLocate(files, file => pathExists(file));

console.log(foundPath);
//=> 'rainbow.png'

The above is just an example. Use locate-path if you need this.

API

pLocate(input, tester, options?)

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.

input

Type: Iterable<Promise | unknown> | AsyncIterable<unknown>

An iterable or async iterable of promises/values to test.

When an AsyncIterable is given, it is iterated serially and the concurrency and preserveOrder options are not applicable.

tester(element)

Type: Function

This function will receive resolved values from input and is expected to return a Promise<boolean> or boolean.

options

Type: object

concurrency

Type: number
Default: Infinity
Minimum: 1

The number of concurrently pending promises returned by tester.

preserveOrder

Type: boolean
Default: true

Preserve input order when searching.

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

  • p-map - Map over promises concurrently
  • p-filter - Filter promises concurrently
  • p-any - Wait for any promise to be fulfilled
  • More…

版本与发布记录

p-locate 共发布 8 个版本,首次发布于 2016/11/20;过去 12 个月发布了 1 个版本。

版本发布日期距今
7.0.0latest2026/02/039 个月前
6.0.02021/08/255 年前
5.0.02020/08/116 年前
4.1.02019/04/047 年前
4.0.02019/03/127 年前
3.0.02018/06/158 年前

关于 p-locate 的常见问题

p-locate 是什么?

Get the first fulfilled promise that satisfies the provided testing function

如何安装 p-locate?

在项目目录运行 npm install p-locate,或使用 pnpm add / yarn add / bun add。

p-locate 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

p-locate 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

p-locate 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

p-locate 可以商用吗?

p-locate 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

p-locate 的下载量有多少?

根据 npm 官方下载统计,p-locate 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,264,116,200 次,日均约 42,137,207 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

p-locate 还在维护吗?

最新版本 7.0.0 发布于 2026/02/03(9 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 1 个;首次发布于 2016/11/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。