Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save manavm1990/100ecf1b232078fb8fdc9f2042d28f29 to your computer and use it in GitHub Desktop.
Save manavm1990/100ecf1b232078fb8fdc9f2042d28f29 to your computer and use it in GitHub Desktop.
async function example
// 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