Creating promises with an async function
The async
functions are an easy way to create and work with promises. In this recipe, we'll see a basic form of this.
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-01-creating-Promise-with-async
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
with anasync
function namedsomeTask
:
// main.js async function someTask () { console.log('Performing some task'); }
- Create a
main
that callssomeTask
and logs messages before and aftersomeTask
is executed:
export function main () { console.log('before task'); someTask(); console.log('after task created'); }
- Chain a
then
call off ofsomeTask
and log a message in the callback function:
export function main () { console...