


What are the two ways to synchronize WeChat applet execution in sequence?
Foreword:
There are two ways to synchronize the execution of small programs in sequence:
The first way: callback function execution, the latter method Write to the previous callback function to achieve sequential execution
Disadvantages: too much nesting, code confusion
Second method: async-await synchronous execution, this method waits for the previous method Continue execution only after execution is completed
Advantages: High code readability
Take checking text security as an example and give two different methods of code for reference
async- await
/** * 同步检查是否包含敏感词 */ // async function checkString(content) { // try { // var res = await wx.cloud.callFunction({ // name: 'checkString', // data: { // content: content, // } // }); // if (res.result.errCode == 0) // return true; // return false; // } catch (err) { // console.log(err); // return false; // } // } // pubcom: async function (e) { // wx.showLoading({ // title: '加载中', // mask: true // }) // var that = this // var doc_id = that.data.commentID // var content = that.data.comcon // var formId = e.detail.formId; // if (!content) { // return // } // var isCheck = await common.checkString(content); // if (!isCheck) { // wx.showToast({ // title: '含有敏感词', // image: "/assets/icon/icon-warning.png", // }); // return // } //后续代码
(Learning video sharing: php video tutorial)
Callback method
/** * 异步检查 */ function checkString(content,success,fail){ wx.cloud.callFunction({ name: 'checkString', data: { content: content, } }).then(res => { console.log(res); if (res.result.errCode == 0) success(res); }).catch(err => { console.error(err); fail(err); }); } pubcom: function (e) { wx.showLoading({ title: '加载中', mask: true }) var that = this var content = that.data.comcon if (!content) { return } common.checkString(content, function (res) { //成功代码 }, function (err) { //失败 wx.showToast({ title: '含有敏感词', image: "/assets/icon/icon-warning.png", }); return}); },
Related recommendations: 小program development tutorial
The above is the detailed content of What are the two ways to synchronize WeChat applet execution in sequence?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
