微信小程序上 promise 使用
1、引用此js,没有自行百度
import regeneratorRuntime from "sudu8_page/resource/js/runtime.js";
2、js代码:
//一、同步执行: async await 关键词
调用方也得加 async
async testAsync() {
const result = await this.testPromise()
console.log('async test--', result)
},
Promise handle
async test-- 1234
//二、简单的
testPromise() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('Promise handle') resolve(1234) //提前返回回去,表示正确数据 then res //reject(33333) //提前返回回去,表示异常内容 catch err }, 2000) }) }, a:function(){ this.testPromise().then(res=>{ console.log(res) }).catch(function(err){ console.log(err) //如果有 reject 则打印这个的值 }) }打印:
1234
or
33333