Created
July 7, 2017 23:48
-
-
Save dalaidunc/b854dcbc9b1affb2cadaf7bc9f6e0964 to your computer and use it in GitHub Desktop.
Async / Await simple 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
| /* === 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