用Promise定义一个异步任务
function async() {
return new Promise(function(resolve, reject) {
// 执行异步任务
if(success)
resolve("得到些数据") // 将promise置为成功状态
else
reject("失败了") // 将promise置为失败状态
})
}
then用来执行resolve回调
runAsync().then(funciton(data){
console.log(data) // 得到些数据
})
catch
catch不但用来捕获reject后的失败情况,而且还能捕获then里的异常
runAsync().then(function(data){
console.log(data) //得到些数据
}).catch(function(reason) {
console.log(reason) //失败了
})