From efcd372d89d9948ce13eac083e2c2339ff26a7f6 Mon Sep 17 00:00:00 2001 From: Geo Halkiadakis Date: Fri, 24 Mar 2023 17:16:38 +0200 Subject: linkeysearch: javascript scripts keygen+freq can deploy as Google Cloud Funcctions --- javascript/async.js | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 javascript/async.js (limited to 'javascript/async.js') diff --git a/javascript/async.js b/javascript/async.js new file mode 100644 index 0000000..561270c --- /dev/null +++ b/javascript/async.js @@ -0,0 +1,118 @@ +/** example file + * + * this script tests async chain handling; + * runnig async functions sequentialy + * or running in parallel + * + */ + + +/** + * functions a and b are asyncohronus + */ + +async function a(msg) { + sum = 0; + for (i=0 ; i< parseInt(Math.floor(Math.random() * 1000000000)); i++) { + sum += i; + } + console.log(msg, sum); + return sum +} + +async function b(upto) { + sum = 0; + for (i=0 ; i< upto; i++) { + sum += i; + } + console.log(upto, sum); + return new Promise((resolve, reject) => { resolve(sum); }); +} + + +/** test + * --- -- -- - - - + * is an asynchronous function + * that calls multiple times in parallel + * the a and b functions + */ +async function test() { + labels = ['one', 'two', 'three', 'four', 'five']; + labels.forEach( lab => { + a(lab) + }); + console.log('test1'); + + limits = [1000000, 50000, 30000000, 500, 8]; + limits.forEach( lim => { + b(lim).then(console.log(lim,'is done!')); + }); + console.log('test2'); +} + + +test() // run test (many async functions in parallel) +.then(() => { // after test is ended + console.log('test parts(1+2) ended'); + + // run async functions in sequentialy + a('more') + .then(() => { + b(100000) + .then(() => { + a('last') + .then(() => { + b(20) + .then(() =>{ + console.log('exiting...'); + console.log('\n\n--------------------\nNext please...\n--------------------\n\n'); + }) + }); + }); + }); + +}); + + + +/** + * xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + * ----------------------------------------------------------------------------- + * + * * * * * * * * * * * * S E C O N D E X A M P L E * * * * * * * * * * * * + * + * ----------------------------------------------------------------------------- + * xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + */ + + +/** sleep + * --- -- -- - - - + * an async function with predictable waiting time + */ +async function sleep(time = 1) { + const sleepMilliseconds = time * 1000; + return new Promise((resolve, reject) => { + setTimeout(() => { + console.log(`Slept for: ${sleepMilliseconds}ms`); + resolve(sleepMilliseconds); + }, sleepMilliseconds); + }); +} + +// run all in parallel +Promise.all([ // after each call is completed + // '.then(...)' make a message note + sleep(3).then(()=>{ console.log('instance', 3, 'completed')}), + sleep(2).then(()=>{ console.log('instance', 2, 'completed')}), + sleep(1).then(()=>{ console.log('instance', 1, 'completed')}) + +]) +.then( () => { // then + console.log('Now, there should be all done!', 'DONE!') +}); + + +// message to test that async functions keep running on the background +console.log('--> is this the end?', 'NOPE! <--'); + -- cgit v1.2.3