search

Home  >  Q&A  >  body text

javascript - How to implement the following flow control scheme

巴扎黑巴扎黑2798 days ago675

reply all(2)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-31 10:43:05

    I was very touched by this code, so I typed GG. You can directly enter the following code in the console to see the result. . .

    function async(arr){
        if( Object.prototype.toString.call(arr) != "[object Array]" ){
            return false;
        }
        var target = arr.shift();
        if(typeof target != "function"){
            return false;
        }
        target(function(){
            async(arr);
        });
    }
    
    var one = function(callback){
        setTimeout(function(){
            console.log("first");
            callback();
        },200);
    };
    
    var two = function(callback){
        setTimeout(function(){
            console.log("second");
            callback();
        },100);
    };
    
    async([one,two]);
    async([one,two,two,one,two]);

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-31 10:43:05

    I really want to ask, there must be a url for asynchronous use. You can write the url as a function. . .

    reply
    0
  • Cancelreply