Using async results in a Promise chain
In the previous recipe, we saw how to use async
and await
to replace portions of the Promise API. However, there will still be cases where it is preferable to use the Promise API, either for clarity, structure, or incremental replacement.
In this recipe, we'll see how the async
functions integrate seamlessly into Promise chains.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
04-03-async-function-Promise-chain
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
.
- Create an
async
function,getRandomNumber
, that returns a random number:
async function getRandomNumber() { console.log('Getting random number.'); return Math.random(); }
- Create an
async
function,determinReadyToLaunch
, that returnstrue...