diff options
| author | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-24 17:16:38 +0200 |
|---|---|---|
| committer | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-24 17:16:38 +0200 |
| commit | efcd372d89d9948ce13eac083e2c2339ff26a7f6 (patch) | |
| tree | f842c79e03f663a3af34b8b521610eab82a80183 /javascript/async.js | |
| parent | 9754576755da07c9af3a466cd00d99b29e5ca8dc (diff) | |
| download | linkeysearch-efcd372d89d9948ce13eac083e2c2339ff26a7f6.tar.gz linkeysearch-efcd372d89d9948ce13eac083e2c2339ff26a7f6.tar.bz2 linkeysearch-efcd372d89d9948ce13eac083e2c2339ff26a7f6.zip | |
linkeysearch: javascript scripts keygen+freq can deploy as Google Cloud Funcctions
Diffstat (limited to 'javascript/async.js')
| -rw-r--r-- | javascript/async.js | 118 |
1 files changed, 118 insertions, 0 deletions
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! <--'); + |
