![ECMAScript Cookbook](https://wfqqreader-1252317822.image.myqcloud.com/cover/58/36700058/b_36700058.jpg)
上QQ阅读APP看书,第一时间看更新
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named 4-02-await-async-results.
- Copy or create an index.html that loads and runs a main function from main.js.
- Create a main.js with an async function named getRandomNumber that returns a random number:
// main.js async function getRandomNumber () { return Math.random(); }
- Create an async function, main, that calls getRandomNumber, waits for the result, and logs out the value:
export async function main () { console.log('before task'); const result = await getRandomNumber(); console.log('Received the value: %s', result); console.log('after task completed'); }
- Start you Python web server and open the following link in your browser:
http://localhost:8000/. - You should see the following output:
![](https://epubservercos.yuewen.com/C944E7/19470395501577006/epubprivate/OEBPS/Images/316680de-d8e2-445a-bd82-cc268ebe4367.png?sign=1739608124-YkJM2E4EgHBNv1gIm0RBT68bREXzdV5N-0-0a11c0b25dafcbc2836ddcf8c887e0cc)