Skip to content

Instantly share code, notes, and snippets.

@devcybiko
Last active July 9, 2021 02:49
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 devcybiko/b55307b6e719360078c51e96ff4c1afe to your computer and use it in GitHub Desktop.
Save devcybiko/b55307b6e719360078c51e96ff4c1afe to your computer and use it in GitHub Desktop.
demonstration of how async/await is still asynchronous
const { access } = require('fs').promises;
const { constants } = require('fs');
const readline = require('readline');
const prompt = require('prompt-sync')();
let count = 10;
async function timer$(s) {
console.log(s + " " + count);
count--;
if (count) setTimeout(timer$, 1000, s);
}
async function access$(fname) {
try {
await access(fname, constants.R_OK | constants.W_OK);
console.log('can access');
} catch {
console.error('cannot access');
}
}
function questionExample() {
const name = prompt('What is your name?');
console.log(`Hey there ${name}`);
}
function ask() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve, reject) => {
rl.question('Enter input: ', (input) => {
rl.close();
resolve(input);
});
});
}
async function questionExample$() {
const answer = await ask('What is your favorite food? ');
console.log(`Oh, so your favorite food is ${answer}`);
}
async function main$() {
let async = process.argv[2] === "async";
await timer$("tick");
if (async) await questionExample$();
else questionExample();
await access$("/etc/passwd");
}
main$();
@devcybiko
Copy link
Author

devcybiko commented Jul 9, 2021

asyncExample

This demonstrates that async/await performs asynchronously, despite the fact that the code looks synchronous

When run with the async parameter everything is run asynchronously. Without it, the questionExample() function calls the prompt() function which is synchronous and hangs the works.

Run synchronously

node asyncExample.js

  • For clarity - async functions are named with a dollar sign$
  • the questionExample() function is synchronous
  • when running on CLI without the async parameter, the questionExample() is called
  • questionExample() calls the prompt() function which is synchronous and hangs the works.
  • This hangs everything - the timer$() function doesn't continue to run
  • once the user enters their name, the asynchronous code continues to run
  • once the timer counts down to zero, it stops perpetuating the timer and the whole program exits

Run asynchronously

node asyncExample.js async

  • For clarity - async functions are named with a dollar sign$
  • the questionExample$() function is asynchronous
  • when running on CLI with the async parameter, the questionExample$() function is called
  • questionExample$() calls readline.question() asynchronously
  • This allows everything to run asynchronously
  • the user can enter their favorite food and everything continues to run async
  • once the timer counts down to zero, it stops perpetuating the timer and the whole program exits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment