async ,await 和 promise在项目中的实践应用

149 阅读1分钟
function exc(){
     return new Promise(function(resolve, reject) {
        setTimeout(async()=>{
            let a = 111111
            resolve(a);
        },1000);
    }) 
}

async function test(){

     var a = await exc();
          console.log(a)
}

test()





  
// 并行
Promise.all(paths.map(path => pathToBase64(path)))
.then(res => {
  console.log(res)
  // [base64, base64...]
})
.catch(error => {
  console.error(error)
})
// 串行
paths.reduce((promise, path) => 
promise.then(res => pathToBase64(path).then(base64 => (res.push(base64), res))), Promise.resolve([]))
.then(res => {
  console.log(res)
  // [base64, base64...]
})
.catch(error => {
  console.error(error)
})