ES2017 async/await

ES2107 has new methods for handling promises: async & await.  Due to the asynchronous nature of Javascript, when I call an api, it will likely return the api data last, even if it is called at the beginning of the code.  I’m taking this example from Colt Steele’s Advanced JS Bootcamp on Udemy.

  1. Return one movie from OMDB using async/await

This is a call to the omdb movie database to return info on the movie Titanic.  As you can see, on line 1, I add the async keyword in front of ‘function’, and then on line 3 I add await in front of the api call.  When I use await in front of the api call, it returns a promise with the status of ‘pending’ (line 11).  Once the promise is resolved, the function then console.logs the movieData, and then the ‘all done!’ message.

2. Return multiple movies from OMDB using async/await

If I am doing multiple async calls, I can use  await Promise.all.  As above, I add the async keyword in front of function.  Then on line 2, instead of using await for a single api call, I can wrap multiple api calls in an array using Promise.all.

It prints out the plot of “This Disaster Artist”, a great film I just saw today, and “About a boy” which is one of my all-time favorite movies.

3. Find github user with most followers from unspecified number of users

3. Find Star Wars character from swapi.co

 

Leave a Reply

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