• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Twitter GitHub
You are here: Home / Draft / Promise and Async / Await

Promise and Async / Await

Last updated on August 7, 2020 by Sal Ferrarello

Example using a Promise

var getPosts = () => {
    fetch('/wp-json/wp/v2/posts')
        .then((response) => response.json())
        .then((json) => { console.log(json); });
};

Example using async / await

Note: await only works when inside an async function.

var getPosts = async () => {
    var response = await fetch('/wp-json/wp/v2/posts');
    var json = await response.json()
    console.log(json);
};
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

TwitterFacebookLinkedInEmailReddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Draft, Programming Tagged With: JavaScript, JS async / await, JS Promise

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2021 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in