Skip to content

Instantly share code, notes, and snippets.

@peterbean410
Last active October 4, 2022 10:41
Show Gist options
  • Save peterbean410/6ae0534f972dbb82b923dca0e9684077 to your computer and use it in GitHub Desktop.
Save peterbean410/6ae0534f972dbb82b923dca0e9684077 to your computer and use it in GitHub Desktop.
How does async work in NodeJS
async function callme(message, level) {
console.log(message, "entering", level)
level--;
// let req = new XMLHttpRequest();
// req.open('GET', "https://thumbs.dreamstime.com/b/brown-dog-corso-corso-stands-field-green-grass-brown-dog-corso-cors-120197441.jpg");
// req.onload = function () {
// if (req.status == 200) {
// console.log(req.status, "Download ok");
// } else {
// console.log("File not Found");
// }
// };
// req.send();
for (let i = 0; i < 100; i++) {
console.log(message, "Looping to " + i + " at level: " + level)
// if (level > 0) {
// callme("in forloop", -1);
// console.log("###")
// }
}
}
const started = Date.now();
console.log("Started: " + started);
setTimeout(function(){
const inCallback = Date.now();
console.log("In setTimeout callback: " + inCallback, inCallback - started);
console.log("Called after " + "10 milsec");
}, 0);
callme("Not important", -1);
callme("In main", -1).then(() => new Promise((res) => {
return res(1)
})).then((val) => {
console.log('log', val);
});
console.log("Should print");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment