Skip to content

Instantly share code, notes, and snippets.

@dalaidunc
Created July 7, 2017 23:48
Show Gist options
  • Select an option

  • Save dalaidunc/b854dcbc9b1affb2cadaf7bc9f6e0964 to your computer and use it in GitHub Desktop.

Select an option

Save dalaidunc/b854dcbc9b1affb2cadaf7bc9f6e0964 to your computer and use it in GitHub Desktop.
Async / Await simple example
/* === EXAMPLE 1 === */
// Create a simple Promise wrapper for setTimeout
function wait (ms) {
return new Promise (resolve => setTimeout(resolve, ms));
}
// Declare an async function
async function hello (name) {
// wait for the specified time
// Note: The await keyword can only be used within an async function
await wait(1000);
console.log(`Hello ${name}!`);
}
hello('Duncan');
/* === EXAMPLE 2 === */
async function notReallyAsync (name) {
// you can call await on synchronous functions
await console.log('not really async');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment