p-limit vs async vs bluebird vs promise-limit vs promise-queue
JavaScript 中的異步處理
p-limitasyncbluebirdpromise-limitpromise-queue類似套件:

JavaScript 中的異步處理

在 JavaScript 中,異步處理是指在不阻塞主執行緒的情況下執行任務的能力。這對於處理 I/O 操作、網絡請求或任何需要時間才能完成的任務至關重要。這些庫提供了各種工具和方法來簡化異步編程,管理回調函數,處理承諾(Promises),以及控制並發執行。這使得開發人員能夠更有效地編寫非阻塞代碼,從而提高應用程序的性能和響應能力。

npm下載趨勢

3 年

GitHub Stars 排名

統計詳情

套件
下載數
Stars
大小
Issues
發布時間
許可
p-limit461,131,1332,94316.1 kB02 個月前MIT
async146,162,03728,120808 kB472 年前MIT
bluebird83,643,88820,458-1317 年前MIT
promise-limit9,522,898142-108 年前ISC
promise-queue1,220,482230-109 年前MIT

功能比較: p-limit vs async vs bluebird vs promise-limit vs promise-queue

並發控制

  • p-limit:

    p-limit 專注於限制同時執行的承諾數量,這使得它在防止過載和控制資源使用方面非常有效。

  • async:

    async 庫提供了一些方法來控制並發,例如 async.eachLimit 和 async.parallelLimit,但這些功能並不是它的主要焦點。

  • bluebird:

    bluebird 提供了 Promise.map 方法,允許您設置並發限制,這使得它在處理大量異步操作時非常靈活。

  • promise-limit:

    promise-limit 提供了一個簡單的 API 來限制同時執行的承諾數量,但功能相對較少,適合輕量級使用。

  • promise-queue:

    promise-queue 則是通過隊列的方式按順序執行承諾,確保每個承諾在前一個完成後才開始。

承諾取消

  • p-limit:

    p-limit 不支持承諾取消,因為它專注於限制並發數量,而不是管理承諾的生命週期。

  • async:

    async 庫本身不支持承諾取消,但您可以通過回調函數來實現類似的功能。

  • bluebird:

    bluebird 提供了對承諾取消的內建支持,這使得它在需要中止異步操作的場景中非常有用。

  • promise-limit:

    promise-limit 也不支持承諾取消,這是一個簡單的庫,沒有實現這個功能。

  • promise-queue:

    promise-queue 不支持承諾取消,但您可以通過管理隊列來實現類似的功能。

錯誤處理

  • p-limit:

    p-limit 對錯誤處理的支持較少,因為它主要專注於並發控制。您需要手動處理承諾中的錯誤。

  • async:

    async 庫提供了良好的錯誤處理機制,特別是在使用回調函數時。它允許您在異步操作中傳遞錯誤,並提供了 async.series 和 async.parallel 等方法來處理錯誤。

  • bluebird:

    bluebird 提供了強大的錯誤處理功能,包括鏈式錯誤處理和錯誤捕獲。它還支持 catch 和 finally 方法,使得錯誤處理更加靈活。

  • promise-limit:

    promise-limit 也不提供專門的錯誤處理機制,錯誤處理需要依賴於承諾的標準實現。

  • promise-queue:

    promise-queue 允許您在隊列中處理錯誤,但錯誤處理需要由使用者自行實現。

性能

  • p-limit:

    p-limit 是一個輕量級的庫,對性能的影響非常小。它只在限制並發時引入開銷,這使得它在需要控制資源使用的場景中非常高效。

  • async:

    async 庫的性能取決於您使用的具體方法。對於大量回調操作,性能可能會受到影響,但它提供了許多優化方法來減少這種影響。

  • bluebird:

    bluebird 是已知的高性能承諾實現,特別是在處理大量異步操作時。它的性能優於原生承諾,並且在大多數情況下都能提供快速的執行速度。

  • promise-limit:

    promise-limit 也對性能影響很小,因為它只是在限制並發執行的承諾。

  • promise-queue:

    promise-queue 的性能取決於隊列的大小和執行速度。由於它是按順序執行承諾的,因此在處理大量承諾時可能會導致延遲。

Ease of Use: Code Examples

  • p-limit:

    使用 p-limit 限制並發

    const pLimit = require('p-limit');
    const limit = pLimit(2);
    const tasks = Array.from({ length: 5 }, (_, i) => () => new Promise(resolve => setTimeout(() => resolve(`任務 ${i + 1}`), 1000)));
    const limitedTasks = tasks.map(task => limit(task));
    Promise.all(limitedTasks).then(console.log);
    
  • async:

    使用 async 庫進行並行處理

    const async = require('async');
    
    async.parallel([
      function(callback) {
        setTimeout(() => callback(null, '結果 1'), 1000);
      },
      function(callback) {
        setTimeout(() => callback(null, '結果 2'), 500);
      }
    ],
    function(err, results) {
      if (err) throw err;
      console.log(results);
    });
    
  • bluebird:

    使用 bluebird 進行並發限制

    const Promise = require('bluebird');
    const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
    
    const tasks = Array.from({ length: 10 }, (_, i) => () => sleep(1000).then(() => `任務 ${i + 1}`));
    
    const limitedConcurrent = Promise.map(tasks, (task) => task(), { concurrency: 3 });
    
    limitedConcurrent.then(console.log);
    
  • promise-limit:

    使用 promise-limit 限制並發

    const { limit } = require('promise-limit')(2);
    const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
    const tasks = Array.from({ length: 5 }, (_, i) => () => sleep(1000).then(() => `任務 ${i + 1}`));
    const limitedTasks = tasks.map(task => limit(task));
    Promise.all(limitedTasks).then(console.log);
    
  • promise-queue:

    使用 promise-queue 按順序執行承諾

    const Queue = require('promise-queue');
    const queue = new Queue(1); // 1 表示每次只執行一個
    const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
    const tasks = Array.from({ length: 5 }, (_, i) => () => sleep(1000).then(() => `任務 ${i + 1}`));
    const queuedTasks = tasks.map(task => queue.add(task));
    Promise.all(queuedTasks).then(console.log);
    

如何選擇: p-limit vs async vs bluebird vs promise-limit vs promise-queue

  • p-limit:

    如果您需要控制並發數量以防止過載系統,請選擇 p-limit。它是一個輕量級的庫,專注於限制同時執行的承諾數量,適合需要對資源使用進行精細控制的場景。

  • async:

    如果您需要一個功能全面的庫來處理回調、承諾和事件,並且不介意引入較大的依賴,請選擇 async。它提供了豐富的 API 來管理異步流程,特別是在處理複雜的回調地獄時。

  • bluebird:

    如果您需要高性能的承諾實現,並且需要許多附加功能(如承諾取消、進度通知等),請選擇 bluebird。它特別適合需要大量異步操作的應用程序,並且提供了比原生承諾更好的性能和功能。

  • promise-limit:

    如果您需要一個簡單的解決方案來限制同時執行的承諾數量,請選擇 promise-limit。它提供了一個簡單的 API 來設置並發限制,適合不需要額外功能的輕量級應用。

  • promise-queue:

    如果您需要按順序執行承諾並確保它們一個接一個地完成,請選擇 promise-queue。它適合需要嚴格控制執行順序的場景,並且提供了簡單的 API 來管理隊列。

p-limit 的 README

p-limit

Run multiple promise-returning & async functions with limited concurrency

Works in Node.js and browsers.

Install

npm install p-limit

Usage

import pLimit from 'p-limit';

const limit = pLimit(1);

const input = [
	limit(() => fetchSomething('foo')),
	limit(() => fetchSomething('bar')),
	limit(() => doSomething())
];

// Only one promise is run at once
const result = await Promise.all(input);
console.log(result);

API

pLimit(concurrency) default export

Returns a limit function.

concurrency

Type: number | object
Minimum: 1

Concurrency limit.

You can pass a number or an options object with a concurrency property.

rejectOnClear

Type: boolean
Default: false

Reject pending promises with an AbortError when clearQueue() is called. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

import pLimit from 'p-limit';

const limit = pLimit({concurrency: 1});

limit(fn, ...args)

Returns the promise returned by calling fn(...args).

fn

Type: Function

Promise-returning/async function.

args

Any arguments to pass through to fn.

Support for passing arguments on to the fn is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a lot of functions.

Warning: Avoid calling the same limit function inside a function that is already limited by it. This can create a deadlock where inner tasks never run. Use a separate limiter for inner tasks.

limit.map(iterable, mapperFunction)

Process an iterable of inputs with limited concurrency.

The mapper function receives the item value and its index.

Returns a promise equivalent to Promise.all(Array.from(iterable, (item, index) => limit(mapperFunction, item, index))).

If the iterable throws during iteration, the returned promise rejects with that error. Mapper functions that were already scheduled still run, and their results are discarded.

This is a convenience function for processing inputs that arrive in batches. For more complex use cases, see p-map.

limit.activeCount

The number of promises that are currently running.

limit.pendingCount

The number of promises that are waiting to run (i.e. their internal fn was not called yet).

limit.clearQueue()

Discard pending promises that are waiting to run.

This might be useful if you want to tear down the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.

Note: This does not cancel promises that are already running.

When rejectOnClear is enabled, pending promises are rejected with an AbortError. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

limit.concurrency

Get or set the concurrency limit.

limitFunction(fn, options) named export

Returns a function with limited concurrency.

The returned function manages its own concurrent executions, allowing you to call it multiple times without exceeding the specified concurrency limit.

Ideal for scenarios where you need to control the number of simultaneous executions of a single function, rather than managing concurrency across multiple functions.

import {limitFunction} from 'p-limit';

const limitedFunction = limitFunction(async () => {
	return doSomething();
}, {concurrency: 1});

const input = Array.from({length: 10}, limitedFunction);

// Only one promise is run at once.
await Promise.all(input);

fn

Type: Function

Promise-returning/async function.

options

Type: object

concurrency

Type: number
Minimum: 1

Concurrency limit.

rejectOnClear

Type: boolean
Default: false

Reject pending promises with an AbortError when clearQueue() is called. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

limitedFunction.clearQueue()

Discard pending calls that have not started yet.

When rejectOnClear is enabled, pending promises are rejected with an AbortError. Otherwise, their promises remain unresolved.

This does not cancel calls that are already running.

Recipes

See recipes.md for common use cases and patterns.

FAQ

How is this different from the p-queue package?

This package is only about limiting the number of concurrent executions, while p-queue is a fully featured queue implementation with lots of different options, introspection, and ability to pause the queue.

Related

  • p-throttle - Throttle promise-returning & async functions
  • p-debounce - Debounce promise-returning & async functions
  • p-map - Run promise-returning & async functions concurrently with different inputs
  • p-all - Run promise-returning & async functions concurrently with optional limited concurrency
  • More…