Javascript - how to control how many promises access network in parallel

前端 未结 2 474
天涯浪人
天涯浪人 2020-11-27 23:32

in my application, I have and array of promises that access network in parallel, but some times, when my app is running full speed, my network slows down, due to many promi

2条回答
  •  渐次进展
    2020-11-28 00:14

    This is one way of achieving your aim without using a library. Within the promise returned from makeMaxConcurrencyRequests(), the startNew() function is recursively called, sending new requests until we have been through every id, and without exceeding a current request count of maxConcurrency.

    When each request completes, its return data is pushed into the returnedData array. When all requests are completed, the promise is resolved with returnedData.

    I haven't tested this, but looking at it my only concern is that startNew() is going to be called multiple times in quick succession while requests are pending. If this causes issues then rather than immediately calling startNew(), we could use setTimeout to delay the next invocation - this is commented out in my code.

    function makeMaxConcurrencyRequests(ids, maxConcurrency) {
        return new Promise(function(resolve, reject) {
            let i = 0, currentlyRunning = 0, returnedData = [];
            function startNew() {        
                while (i < ids.length && currentlyRunning <= maxConcurrency) {
                    makeRequest(ids[i++]).then(function(data) {
                        returnedData.push(data);
                        currentlyRunning--;
                        startNew();
                    }).catch(function(err) {
                        reject(err);
                    });
                    currentlyRunning++;
                }
                if (i >= ids.length && currentlyRunning === 0) {
                    resolve(returnedData);
                }
                startNew();
                // setTimeout(startNew, 200);           
            }
        }
    }
    
    function makeRequest(id) {
        return new Promise(function(resolve, reject){
            http.post({url: addr, form: { data: dp }}, function(err, res, body){
                if (err){
                    reject(err)
                } 
    
                http.post({url: hostAddress, form: { data: body.xx }}, function(err2, res2, body2){
                    if(err2) {
                        reject(err2);
                    }
                    resolve(body2.xx);
               }); 
           });
    
       });
    }
    

    Usage:

    var ids = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 6: 56, 7: 7, 8: 8, 5:6 };
    var maxConcurrency = 3;
    makeMaxConcurrencyRequests(Object.keys(ids), maxConcurrency)
    .then(function(data) {
        // do something with data
    }).catch(function(error) {
        // do something with error
    });
    

提交回复
热议问题