Search code snippets, questions, articles...

Javascript Async/Await ES7

async function foo() {
    const myPromise = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("Parwinder"); // resolves with "Parwinder" after 2 seconds
        }, 2000);

    // will not move to the next line until myPromise resolves/rejects
    const name = await myPromise;
    // the execution pauses (or awaits) for the promise

    console.log(name); // Parwinder


Javascript Async/Await ES7 

Async/Await was introduced in ES7 to promote a cleaner syntax to promises. Under the hood, async/await are promises; they provide a nice abstraction layer under those keywords.

Search Index Data (The code snippet can also be found with below search text)

Javascript Async/Await ES7
Was this helpful?
Programming Feeds
Learn something new everyday on Devsheet