`

javascript waitfor function

 
阅读更多

Javascript has many asynchronous functions. Sometimes, we need to wait until the asynchronous function returns than go on. Following give an sample function for waitfor

waitfor = function(){
    var preFns = arguments;
    var thenFn = function(){
        var postFns = arguments;
        var count_down = preFns.length;
        var done = function(){
            count_down--;
            if(count_down == 0 ){
                _.each(postFns, function(postFn){ postFn();});
            }
        };
        _.each(preFns, function(preFn){
            preFn(done);
        });
    }
    return {then: thenFn};
}

---------------------------------
        waitfor(
            function(done) { loadXXX.call(me, done);},
            function(done) { loadYYY.call(me, done); }
        ).then(
            function() {
                setDefaultCriteria.call(me);
                registerWatchForZZZ.call(me);
            }
        );
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics