How to use async/await in WeChat applet

1. Introduce generator support library

The code translated by Babel will implement the function of aysnc/await in a way similar to the co library, that is, to use the generator. Therefore, like when we use co, we need to rely on a regeneratorRuntime to support the generator feature. We can use Facebook's open source regenerator library. You can download the regenerator library via npm:

npm install regenerator --save

Then take out the file named regenerator-runtime in the downloaded file and put it into our applet code.

2. Import code

In the code file that needs to use the async/await feature, introduce the regenerator library:

import regeneratorRuntime from '../../utils/regenerator-runtime/runtime-module.js';

Then, you can safely use async/await to write asynchronous processing in your code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324910099&siteId=291194637
Recommended