Skip to content

Instantly share code, notes, and snippets.

@matthewbordas
Last active October 30, 2018 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewbordas/34f4f0b2428fc7b0b748724f189c2bca to your computer and use it in GitHub Desktop.
Save matthewbordas/34f4f0b2428fc7b0b748724f189c2bca to your computer and use it in GitHub Desktop.
/**
* Going to be testing 3 things:
* 1. Function marked async, but that still runs sync.
* 2. Function marked async and returns a promise, but that still runs sync.
* 3. Function marked async and that runs async by using process.nextTick().
*
* Note: async and sync are defined in the context of the Node.js event loop,
* which technically runs everything on one thread.
*
* Usage: this is a vanillaJS node script. So all you need to to do is type
* "node asyncTest.js" to run this.
*
*/
/**
* Test 1
*
* Function looks async, but actually isn't.
* Marking something async doesn't make it run asynchronously.
*
*/
async function supposedToBeAsync_Test1() {
wait();
finish();
}
/**
* Test 2
*
* Async function does not behave correctly just by using a promise.
* Returning a Promise doesn't make it run asynchronously.
*
*/
async function supposedToBeAyncWithPromise_Test2() {
supposedToBeAyncWithPromiseHelper();
finish();
}
async function supposedToBeAyncWithPromiseHelper() {
return new Promise((resolve, reject) => {
wait(10000);
resolve();
});
}
/**
* Test 3
*
* Async with process.nextTick().
* Runs the wait() call in the next cycle of the event loop.
*
*/
async function asyncWithProcessNextTick_Test3() {
asyncWithProcessNextTickHelper();
finish();
}
async function asyncWithProcessNextTickHelper() {
process.nextTick(() => {
wait(100);
});
}
async function wait(len = 5) {
console.log('Begin - wait()');
let i = 0;
while(i < len) {
console.log(i);
i++;
}
console.log('End - wait()');
}
async function finish() {
console.log('finish()');
}
// #### Uncomment to run test 1 ####
// supposedToBeAsync_Test1();
/**
* Output of test 1 should be:
-----------------------------
Begin - wait()
0
1
...
4
End - wait()
finish()
-----------------------------
Notice how "finish()" is printed last, which
shouldn't happen if "wait()" is async
*/
// #### Uncomment to run test 2 ####
// supposedToBeAyncWithPromise_Test2();
/**
* Output of test 2 should be:
-----------------------------
Begin - wait()
0
1
...
9999
End - wait()
finish()
-----------------------------
Notice how "finish()" is printed last, which
shouldn't happen if "wait()" is async
*/
// #### Uncomment to run test 3 ####
// asyncWithProcessNextTick_Test3();
/**
* Output of test 3 should be:
-----------------------------
finish()
Begin - wait()
0
1
...
99
End - wait()
-----------------------------
Notice how "finish()" is printed first, which
is correct since "wait()" is correctly used async
with the call to process.nextTick()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment