Forked from PH4N70M-0P5/gist:3c01436499d308727fea8cbac6a40c2f
Last active
May 14, 2020 12:10
-
-
Save manavm1990/100ecf1b232078fb8fdc9f2042d28f29 to your computer and use it in GitHub Desktop.
async function example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new PROMISE | |
// This requires 2 PARAMETERS 'resolve' and 'reject' | |
const p = new Promise((resolve, reject) => { | |
// 'setTimeout' to simulate a 1s delay | |
setTimeout(() => resolve('done!'), 1000) | |
}) | |
// DECLARE a new FUNCTION, 'f' as 'async' - need 'async' to use 'await' | |
// There are no PARAMETERS | |
async function f() { | |
// SYNCHRONOUS code | |
let results = 'first!' | |
// AWAIT for the PROMISE to 'resolve' and reassign the 'wrapped' 'results' to 'results' | |
results = await p | |
// What is the result? | |
console.log(results) | |
} | |
// Try this after you have given it some thought! | |
// f() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment