标签:ons func promise async 异步操作 new syn resolve str
promise 是为了解决异步操作(回调地狱)
function one(){
setTimeOut(()=>{
console.log("吃饭")
),3000
}
function two(){
setTimeOut(()=>{
console.log("睡觉")
),3000
}
function two(){
setTimeOut(()=>{
console.log("打豆豆")
),3000
}
用promise
funtion one(){
return new Promise((resolve,reject)=>{
setTimeout((resolve,reject)=>{
resolve("吃饭”)
),1000}
})
}
funtion two(){
return new Promise((resolve,reject)=>{
setTimeout((resolve,reject)=>{
resolve(“睡觉”)
),1000}
})
}
funtion three(){
return new Promise((resolve,reject)=>{
setTimeout((resolve,reject)=>{
resolve(“打豆豆”)
),1000}
})
}
one().then((sucess)=>{
console.log(sucess)
return two()
}).then((sucess)=>{
console.log(sucess)
return three()
}).then((sucess)=>{
console.log(sucess)
})
同时promisr 还可以与async&await结合使用
async function fn(){
const onestr = await one() =>当one中的promise执行完毕后将结果返回;
console.log(onestr)
const twostr = await one() =>当two中的promise执行完毕后将结果返回; console.log(onestr)
const threestr = await one() =>当three中的promise执行完毕后将结果返回; console.log(onestr)
}
标签:ons func promise async 异步操作 new syn resolve str
原文地址:https://www.cnblogs.com/bamboopanders/p/12544015.html