In the previous recipe, we saw how to use promises to execute asynchronous code. However, this code is pretty basic. It just logs a message and then calls resolve
. Often, we want to use asynchronous code to perform some long-running operation, then return that value.
This recipe demonstrates how to use resolve
in order to return the result of a long-running operation.
This recipe assumes that 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.
- Open your command-line application and navigate to your workspace.
- Create a new folder named
3-02-resolving-promise-results.
- Copy or create an
index.html
that loads and runs amain
function frommain.js
.
- Create a
main.js
file that creates a promise and logs messages before and after the promise is created:
// main.js export function main () { console.log('Before promise created'); new Promise(function (resolve...