Skip to content

Instantly share code, notes, and snippets.

@menangen
Last active December 24, 2020 17:18
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 menangen/1fc400739b0c35e8b3d9abeb76051020 to your computer and use it in GitHub Desktop.
Save menangen/1fc400739b0c35e8b3d9abeb76051020 to your computer and use it in GitHub Desktop.
Node.js 14 Promise example in JavaScript
function timeHandler(ok, bad) {
if (Math.random() > 0.5)
ok("Anna")
else
bad("Andre")
}
function
setUp(ok, bad) {
setTimeout(timeHandler, 1000, ok, bad)
}
function
setUpNewPromise() {
return new Promise(ok => {
/*
let x = fetch("://")
ok(x)
*/
setTimeout(ok, 5000)
})
}
async function
startLoop() {
let result,
P = new Promise(setUp);
// P = setUpNewPromise();
try {
result = await P;
console.log(result)
} catch ( error ) {
console.log("Reject:", error)
}
}
Array(10).fill(0).map(startLoop)
console.log("Start\n")
setInterval(() => { console.log("Run V8 interval") }, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment