What is the best way to limit concurrency when using ES6's Promise.all()?

前端 未结 17 792
执念已碎
执念已碎 2020-11-29 21:28

I have some code that is iterating over a list that was queried out of a database and making an HTTP request for each element in that list. That list can sometimes be a rea

17条回答
  •  情书的邮戳
    2020-11-29 22:11

    P-Limit

    I have compared promise concurrency limitation with a custom script, bluebird, es6-promise-pool, and p-limit. I believe that p-limit has the most simple, stripped down implementation for this need. See their documentation.

    Requirements

    To be compatible with async in example

    • ECMAScript 2017 (version 8)
    • Node version > 8.2.1

    My Example

    In this example, we need to run a function for every URL in the array (like, maybe an API request). Here this is called fetchData(). If we had an array of thousands of items to process, concurrency would definitely be useful to save on CPU and memory resources.

    const pLimit = require('p-limit');
    
    // Example Concurrency of 3 promise at once
    const limit = pLimit(3);
    
    let urls = [
        "http://www.exampleone.com/",
        "http://www.exampletwo.com/",
        "http://www.examplethree.com/",
        "http://www.examplefour.com/",
    ]
    
    // Create an array of our promises using map (fetchData() returns a promise)
    let promises = urls.map(url => {
    
        // wrap the function we are calling in the limit function we defined above
        return limit(() => fetchData(url));
    });
    
    (async () => {
        // Only three promises are run at once (as defined above)
        const result = await Promise.all(promises);
        console.log(result);
    })();
    

    The console log result is an array of your resolved promises response data.

提交回复
热议问题