


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

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.