Asynchronous for cycle in JavaScript

前端 未结 13 1106
温柔的废话
温柔的废话 2020-11-22 11:37

I need a loop that waits for an async call before continuing. Something like:

for ( /* ... */ ) {

  someFunction(param1, praram2, function(result) {

    //         


        
13条回答
  •  长情又很酷
    2020-11-22 11:44

    A promise library based solution:

    /*
        Since this is an open question for JS I have used Kris Kowal's Q promises for the same
    */
    
    var Q = require('q');
    /*
        Your LOOP body
        @success is a parameter(s) you might pass
    */
    var loopBody = function(success) {
        var d = Q.defer(); /* OR use your favorite promise library like $q in angular */
        /*
            'setTimeout' will ideally be your node-like callback with this signature ... (err, data) {}
            as shown, on success you should resolve 
            on failure you should reject (as always ...) 
        */
        setTimeout(function(err, data) {
            if (!err) {
                d.resolve('success');
            } else {
                d.reject('failure');
            }
        }, 100); //100 ms used for illustration only 
        return d.promise;
    };
    
    /*
        function to call your loop body 
    */
    function loop(itr, fn) {
        var def = Q.defer();
        if (itr <= 0) {
            def.reject({ status: "un-successful " });
        } else {
            var next = loop.bind(undefined, itr - 1, fn); // 'next' is all there is to this 
            var callback = fn.bind(undefined /*, a, b, c.... */ ); // in case you want to pass some parameters into your loop body
            def.promise = callback().then(def.resolve, next);
        }
        return def.promise;
    }
    /*
        USAGE: loop(iterations, function(){})
        the second argument has to be thenable (in other words return a promise)
        NOTE: this loop will stop when loop body resolves to a success
        Example: Try to upload file 3 times. HURRAY (if successful) or log failed 
    */
    
    loop(4, loopBody).then(function() {
        //success handler
        console.log('HURRAY')
    }, function() {
        //failed 
        console.log('failed');
    });
    

提交回复
热议问题