<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>
标签:console,log,ECM5,var,msg,asynchronous,arg,returnMsg From: https://www.cnblogs.com/jchblog/p/16790965.html