### [微信小程序按顺序同步执行的两种方式分别是什么](https://www.prosaas.cn/article/16432) **Published:** 2021-03-17T03:41:53 **Author:** 5201314 **Excerpt:** 前言:小程序按顺序同步执行有如下两种方式:第一种方式:回调函数执行,后一个方法写到前一个的回调函数中从而实现顺… ![](https://img.php.cn/upload/article/202103/17/2021031711275694432.jpg) 前言: 小程序按顺序同步执行有如下两种方式: 第一种方式:回调函数执行,后一个方法写到前一个的回调函数中从而实现顺序执行 缺点:嵌套太多,代码混轮 第二种方式:async-await 同步执行,此方法等待前面方法执行完毕才继续后续执行 优点:代码可读性高 以检查文本安全性为例给出两种不同方式代码以供参考 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 // } //后续代码 (学习视频分享:php视频教程) 回调方式 /\*\* \* 异步检查 \*/ 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}); }, 相关推荐:小程序开发教程 以上就是微信小程序按顺序同步执行的两种方式分别是什么的详细内容,更多请关注知企PROSAAS其它相关文章! **Categories:** 后端开发 ---