async vs bluebird vs p-limit vs promise-limit vs promise-queue
JavaScript Promise 和异步控制
asyncbluebirdp-limitpromise-limitpromise-queue类似的npm包:

JavaScript Promise 和异步控制

这些库提供了对 JavaScript 中异步操作的不同控制和优化方式。它们各自具有独特的功能,适用于不同的场景。async 是一个功能全面的库,提供了多种异步控制流工具,如并行、串行和限流。bluebird 是一个高性能的 Promise 库,提供了丰富的功能,如并发限制、错误处理和性能优化。p-limit 是一个轻量级的库,专注于限制并发 Promise 的数量,适用于需要简单限流的场景。promise-limit 提供了类似的功能,但更注重 API 的简洁性。promise-queue 是一个专注于队列管理的库,确保 Promise 按照特定顺序执行,适合需要严格顺序的异步操作。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
async87,476,33528,196808 kB242 年前MIT
bluebird42,217,43720,759-1276 年前MIT
p-limit02,81514.9 kB01 个月前MIT
promise-limit0143-108 年前ISC
promise-queue0230-108 年前MIT

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

并发控制

  • async:

    async 提供了多种并发控制机制,如 async.parallelasync.seriesasync.eachLimit,支持灵活的并发和串行执行。

  • bluebird:

    bluebird 通过 Promise.mapPromise.each 提供并发限制功能,允许开发者指定并发数量,适合处理大量异步操作。

  • p-limit:

    p-limit 允许开发者设置并发 Promise 的最大数量,简单易用,适合需要快速限流的场景。

  • promise-limit:

    promise-limit 提供类似的并发限制功能,API 简洁,易于集成。

  • promise-queue:

    promise-queue 通过队列管理确保 Promise 按照顺序执行,适合需要严格控制执行顺序的场景。

错误处理

  • async:

    async 提供了灵活的错误处理机制,支持回调函数中的错误传递,适合复杂的异步流程。

  • bluebird:

    bluebird 提供了强大的错误处理功能,支持链式捕获和自定义错误处理,性能优越。

  • p-limit:

    p-limit 错误处理依赖于 Promise 的原生机制,简单直接。

  • promise-limit:

    promise-limit 也依赖于原生 Promise 错误处理,设计简洁。

  • promise-queue:

    promise-queue 通过队列管理错误,确保错误按照执行顺序处理。

性能

  • async:

    async 的性能开销主要来自于其丰富的功能和灵活性,适合对性能要求不是特别苛刻的场景。

  • bluebird:

    bluebird 是一个高性能的 Promise 实现,特别是在处理大量异步操作时,性能优势明显。

  • p-limit:

    p-limit 由于其轻量级设计,性能开销很小,适合需要高效限流的场景。

  • promise-limit:

    promise-limit 性能开销也很小,适合对性能要求较高的应用。

  • promise-queue:

    promise-queue 通过队列管理执行,性能开销主要来自于队列管理,但整体影响较小。

示例代码

  • async:

    使用 async 进行并发控制

    const async = require('async');
    
    async.eachLimit([1, 2, 3, 4], 2, (item, callback) => {
      setTimeout(() => {
        console.log(item);
        callback();
      }, 1000);
    });
    
  • bluebird:

    使用 bluebird 进行并发限制

    const Promise = require('bluebird');
    
    const tasks = [
      () => new Promise(resolve => setTimeout(() => resolve(1), 1000)),
      () => new Promise(resolve => setTimeout(() => resolve(2), 1000)),
      () => new Promise(resolve => setTimeout(() => resolve(3), 1000)),
    ];
    
    Promise.map(tasks, task => task(), { concurrency: 2 }).then(console.log);
    
  • p-limit:

    使用 p-limit 限制并发

    const pLimit = require('p-limit');
    const limit = pLimit(2);
    
    const tasks = [
      () => new Promise(resolve => setTimeout(() => resolve(1), 1000)),
      () => new Promise(resolve => setTimeout(() => resolve(2), 1000)),
      () => new Promise(resolve => setTimeout(() => resolve(3), 1000)),
    ];
    
    const limitedTasks = tasks.map(task => limit(task));
    Promise.all(limitedTasks).then(console.log);
    
  • promise-limit:

    使用 promise-limit 限制并发

    const limit = require('promise-limit')(2);
    
    const tasks = [
      () => new Promise(resolve => setTimeout(() => resolve(1), 1000)),
      () => new Promise(resolve => setTimeout(() => resolve(2), 1000)),
      () => new Promise(resolve => setTimeout(() => resolve(3), 1000)),
    ];
    
    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 tasks = [
      () => new Promise(resolve => setTimeout(() => resolve(1), 1000)),
      () => new Promise(resolve => setTimeout(() => resolve(2), 1000)),
      () => new Promise(resolve => setTimeout(() => resolve(3), 1000)),
    ];
    
    const queuedTasks = tasks.map(task => queue.add(task));
    Promise.all(queuedTasks).then(console.log);
    

如何选择: async vs bluebird vs p-limit vs promise-limit vs promise-queue

  • async:

    选择 async 如果您需要一个功能全面的库,提供多种异步控制流工具,适用于复杂的异步操作和流程管理。

  • bluebird:

    选择 bluebird 如果您需要一个高性能的 Promise 实现,具有丰富的功能和优化,特别是在处理大量异步操作时。

  • p-limit:

    选择 p-limit 如果您只需要限制并发 Promise 的数量,且希望保持库的轻量和简单。

  • promise-limit:

    选择 promise-limit 如果您需要一个简单易用的限流库,且不需要额外的功能。

  • promise-queue:

    选择 promise-queue 如果您需要确保 Promise 按照特定顺序执行,适合需要严格控制执行顺序的场景。

async的README

Async Logo

Github Actions CI status NPM version Coverage Status Join the chat at https://gitter.im/caolan/async jsDelivr Hits

Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with Node.js and installable via npm i async, it can also be used directly in the browser. An ESM/MJS version is included in the main async package that should automatically be used with compatible bundlers such as Webpack and Rollup.

A pure ESM version of Async is available as async-es.

For Documentation, visit https://caolan.github.io/async/

For Async v1.5.x documentation, go HERE

// for use with Node-style callbacks...
var async = require("async");

var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
var configs = {};

async.forEachOf(obj, (value, key, callback) => {
    fs.readFile(__dirname + value, "utf8", (err, data) => {
        if (err) return callback(err);
        try {
            configs[key] = JSON.parse(data);
        } catch (e) {
            return callback(e);
        }
        callback();
    });
}, err => {
    if (err) console.error(err.message);
    // configs is now a map of JSON data
    doSomethingWith(configs);
});
var async = require("async");

// ...or ES2017 async functions
async.mapLimit(urls, 5, async function(url) {
    const response = await fetch(url)
    return response.body
}, (err, results) => {
    if (err) throw err
    // results is now an array of the response bodies
    console.log(results)
})