ECM5 asynchronous

<script type="text/javascript">
"use strick";

let regularFunction=()=>{
    return "Hello";
}

let timeoutFunction=msg=>{
    var returnMsg;
    setTimeout(()=>{
        console.log("Input msg to timeoutFunction:",msg);
        returnMsg =msg+ "World";
        console.log("timeoutFunction has constructed returnMsg:",returnMsg);
        return returnMsg;
    },10000);
}

let followUpFunction=msg=>{
    console.log("Follow up, Timeout returned:",msg);
}
/*console.log("Before any function call...");
var regularReturn=regularFunction();
var timeoutReturn=timeoutFunction(regularReturn);
followUpFunction(timeoutReturn);
*/
console.log("-----------------test2--------------------");
let timeoutFunctioncallback=(msg,callback)=>{
    var returnMsg;
    //setTimeout不会等待
    setTimeout(()=>{
        console.log("Input msg to timeoutFunctioncallback:",msg);
        returnMsg =msg+ "World";
        console.log("timeoutFunctioncallback has constructed returnMsg:",returnMsg);
    },10000);
    
    callback(returnMsg);
}
/*console.log("Before any function call...");
var regularReturn=regularFunction();
timeoutFunctioncallback(regularReturn,followUpFunction);
*/
console.log("-----------------test3--------------------");
let timeoutFunctionPromise=(msg)=>{
    var returnMsg;
    return new  Promise(resolve=>{
        setTimeout(()=>{
        console.log("Input msg to timeoutFunctionPromise:",msg);
        returnMsg =msg+ "World";
        console.log("timeoutFunctionPromise has constructed returnMsg:",returnMsg);
        resolve(returnMsg);
    },10000);
        
    });
}
/*console.log("The second round of function calls...");
var regularReturn=regularFunction();
timeoutFunctionPromise(regularReturn).then((timeoutReturn)=>{
    followUpFunction(timeoutReturn);
});
*/
let oneFunction =arg=>{
    return new Promise(resolve=>{
        setTimeout(()=>{
        var returnVal=arg;
        console.log("oneFunction produces:",returnVal);
        resolve(returnVal);
    },2000);
    });
};

let twoFunction =arg=>{
    return new Promise(resolve=>{
        setTimeout(()=>{
        var returnVal=arg+"Two";
        console.log("twoFunction produces:",returnVal);
        resolve(returnVal);
    },2000);
    });
};

let threeFunction =arg=>{
    return new Promise(resolve=>{
        setTimeout(()=>{
        var returnVal=arg+"Three";
        console.log("threeFunction produces:",returnVal);
        resolve(returnVal);
    },2000);
    });
};

let finalVal=arg=>{
    return new Promise(resolve=>{
        oneFunction(arg).then((val)=>{
            twoFunction(val).then((val)=>{
                threeFunction(val).then((val)=>{
                    console.log("Final val: ",val);
                })
            })
        })
    });
    
};
//finalVal("One");
let finalValAsync= async arg=>{
    var one=await oneFunction(arg);
    var two=await twoFunction(one);
    var val=await threeFunction(two);
    console.log("Final val: ",val);
}
finalValAsync("One");
</script>

 

posted @ 2022-10-14 11:03  CrazyJC  阅读(526)  评论(0编辑  收藏  举报