Last active
April 11, 2020 12:34
-
-
Save tps2015gh/b5a3a251406a32dec9fc22797d8abe8f to your computer and use it in GitHub Desktop.
javascript demo of using async/await keyword ( with Promise resolve setTimeout )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Thitipong Samranvanich | |
* @since 2561-09-05 | |
* @param {*} ms | |
* @desc | |
* Demo ofy async / await keyword | |
* | |
*/ | |
// ======================================================================================================== | |
// REF : sleep function from | |
// URL https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep | |
// REF : settimeout is not direc return Promise object | |
// URL https://stackoverflow.com/questions/33289726/combination-of-async-function-await-settimeout | |
// REF : async/await keyword | |
// https://medium.com/pnpsolution/%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89%E0%B8%87%E0%B8%B2%E0%B8%99-async-await-%E0%B9%83%E0%B8%99-node-js-b11d3f51689e | |
// https://medium.freecodecamp.org/avoiding-the-async-await-hell-c77a0fb71c4c | |
// https://blog.panjmp.com/async-await-%E0%B9%80%E0%B8%A3%E0%B8%B2%E0%B8%A1%E0%B8%B2%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81-syntax-%E0%B8%97%E0%B8%B5%E0%B9%88%E0%B8%88%E0%B8%B0%E0%B8%A1%E0%B8%B2%E0%B9%80%E0%B8%9B%E0%B8%A5%E0%B8%B5%E0%B9%88%E0%B8%A2%E0%B8%99%E0%B9%82%E0%B8%A5%E0%B8%81%E0%B8%82%E0%B8%AD%E0%B8%87-javascript-%E0%B8%81%E0%B8%B1%E0%B8%99-3f02091eca05 | |
// ======================================================================================================== | |
function sleep2(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
// same as sleep2 , but implicit | |
function sleep(ms){ | |
return new Promise( | |
function(resolve){ | |
setTimeout(resolve,ms) | |
} | |
) | |
} | |
// function random(n){ | |
// return Math.floor(Math.random() * n ); | |
// } | |
async function longRun (fn_name,delay_ms ){ | |
let rnd100 = delay_ms | |
console.log( fn_name + ":JOB /" + rnd100 + "ms ... START ") | |
await sleep(rnd100 ) | |
console.log( fn_name + ":JOB /" + rnd100 + "ms ... finished ") | |
} | |
//============================================== | |
async function run_sync(){ | |
console.log ("===== run SYNC.START ====") | |
await longRun("A1",500) | |
await longRun("A2",50) | |
await longRun("A3",100) | |
console.log ("===== run SYNC.END ====") | |
} | |
//============================================== | |
async function run_async(){ | |
console.log ("===== run ASYNC.START ====") | |
longRun("A1",500) | |
longRun("A2",50) | |
longRun("A3",100) | |
console.log ("===== run ASYNC.END ====") | |
} | |
async function main(){ | |
console.log ("===== SYNC / ASYNC function , with sleep() ====") | |
console.log("\n") | |
await run_sync() | |
console.log("\n"); | |
run_async() | |
var fn = (x) => { return x*2 } | |
var n20 = fn(10) | |
console.log(" === END OF proc main === ") | |
} | |
main() | |
// END OF SCRIPT | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node test_async.js | |
===== SYNC / ASYNC function , with sleep() ==== | |
===== run SYNC.START ==== | |
A1:JOB /500ms ... START | |
A1:JOB /500ms ... finished | |
A2:JOB /50ms ... START | |
A2:JOB /50ms ... finished | |
A3:JOB /100ms ... START | |
A3:JOB /100ms ... finished | |
===== run SYNC.END ==== | |
===== run ASYNC.START ==== | |
A1:JOB /500ms ... START | |
A2:JOB /50ms ... START | |
A3:JOB /100ms ... START | |
===== run ASYNC.END ==== | |
=== END OF proc main === | |
A2:JOB /50ms ... finished | |
A3:JOB /100ms ... finished | |
A1:JOB /500ms ... finished |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment