Two ways to execute applets in sequence
1. The callback function is executed, and the latter method is written into the previous callback function to achieve sequential execution. The disadvantage is that there are too many nesting and the code is confused.
-await Execute synchronously, this method waits for the previous method to be executed before continuing to execute subsequent execution. Good code readability
Taking checking text security as an example, two different codes are given for reference
Callback method
/** * Synchronously check whether sensitive words are included */ // async function checkString(content) { // try { // var res = await ({ // name: 'checkString', // data: { // content: content, // } // }); // if ( == 0) // return true; // return false; // } catch (err) { // (err); // return false; // } // } // pubcom: async function (e) { // ({ // title: 'Loading', // mask: true // }) // var that = this // var doc_id = // var content = // var formId = ; // if (!content) { // return // } // var isCheck = await (content); // if (!isCheck) { // ({ // title: 'Contains sensitive words', // image: "/assets/icon/", // }); // return // } //Subsequent code
async-await
/** * Asynchronous check */ function checkString(content,success,fail){ ({ name: 'checkString', data: { content: content, } }).then(res => { (res); if ( == 0) success(res); }).catch(err => { (err); fail(err); }); } pubcom: function (e) { ({ title: 'loading', mask: true }) var that = this var content = if (!content) { return } (content, function (res) { //Success code }, function (err) { //fail ({ title: 'Contains sensitive words', image: "/assets/icon/", }); return}); },
Summarize
The above are two ways to execute WeChat mini programs in sequence that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website! If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!