這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)ES6中怎么使用Promise對(duì)象,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

在promise之前處理異步回調(diào)的方式
function asyncFun(a,b,callback) {
setTimeout(function () {
callback(a+b);
},200);
}
asyncFun(1,2, function (res) {
if(res > 2) {
asyncFun(res, 2, function (res) {
if(res > 4) {
asyncFun(res, 2, function (res) {
console.log('ok');
console.log(res);
})
}
})
}
});從上面可以看出所謂的”回調(diào)地獄”的可怕
使用promise來優(yōu)雅的處理異步
function asyncFun(a,b) {
return new Promise(function (resolve, reject) {
setTimeout(function() {
resolve(a + b);
},200);
})
}
asyncFun(1,2)
.then(function (res) {
if(res > 2) {
return asyncFun(res, 2);
}
})
.then(function (res) {
if(res > 4) {
return asyncFun(res, 2);
}
})
.then(function (res) {
console.log('ok');
console.log(res);
})
.catch(function (error) {
console.log(error);
});使用promise處理內(nèi)部異常的舉例
function asyncFun(a,b) {
return new Promise(function (resolve, reject) {
// 模擬異常判斷
if(typeof a !== 'number' || typeof b !== 'number') {
reject(new Error('no number'));
}
setTimeout(function() {
resolve(a + b);
},200);
})
}
asyncFun(1,2)
.then(function (res) {
if(res > 2) {
return asyncFun(res, 2);
}
},function (err) {
console.log('first err: ', err);
})
.then(function (res) {
if(res > 4) {
return asyncFun(res, 'a');
}
},function (err) {
console.log('second err: ', err);
})
.then(function (res) {
console.log('ok');
console.log(res);
},function (err) {
console.log('third err: ', err);
});從上面可以看出通過then的第二個(gè)回調(diào)函數(shù)處理promise對(duì)象中的異常,通過reject返回異常的promise對(duì)象
通過catch統(tǒng)一處理錯(cuò)誤,通過finally執(zhí)行最終必須執(zhí)行的邏輯
function asyncFun(a,b) {
return new Promise(function (resolve, reject) {
// 模擬異常判斷
if(typeof a !== 'number' || typeof b !== 'number') {
reject(new Error('no number'));
}
setTimeout(function() {
resolve(a + b);
},200);
})
}
asyncFun(1,2)
.then(function (res) {
if(res > 2) {
return asyncFun(res, 2);
}
})
.then(function (res) {
if(res > 4) {
return asyncFun(res, 'a');
}
})
.then(function (res) {
console.log('ok');
console.log(res);
})
.catch(function (error) {
console.log('catch: ', error);
})
.finally(function () {
console.log('finally: ', 1+2);
});通過Promise.all()靜態(tài)方法來處理多個(gè)異步
function asyncFun(a,b) {
return new Promise(function (resolve, reject) {
setTimeout(function() {
resolve(a + b);
},200);
})
}
var promise = Promise.all([asyncFun(1,2), asyncFun(2,3), asyncFun(3,4)])
promise.then(function (res) {
console.log(res); // [3, 5, 7]
});通過Promise.race()靜態(tài)方法來獲取多個(gè)異步中最快的一個(gè)
function asyncFun(a,b,time) {
return new Promise(function (resolve, reject) {
setTimeout(function() {
resolve(a + b);
},time);
})
}
var promise = Promise.race([asyncFun(1,2,10), asyncFun(2,3,6), asyncFun(3,4,200)])
promise.then(function (res) {
console.log(res); // 5
});通過Promise.resolve() 靜態(tài)方法來直接返回成功的異步對(duì)象
var p = Promise.resolve('hello');
p.then(function (res) {
console.log(res); // hello
});等同于,如下:
var p = new Promise(function (resolve, reject) {
resolve('hello2');
})
p.then(function (res) {
console.log(res); // hello2
});通過Promise.reject() 靜態(tài)方法來直接返回失敗的異步對(duì)象
var p = Promise.reject('err')
p.then(null, function (res) {
console.log(res); // err
});等同于,如下:
var p = new Promise(function (resolve, reject) {
reject('err2');
})
p.then(null, function (res) {
console.log(res); // err
});通過一個(gè)小例子來測(cè)試Promise在面向?qū)ο笾袘?yīng)用
'use strict';
class User{
constructor(name, password) {
this.name = name;
this.password = password;
}
send() {
let name = this.name;
return new Promise(function (resolve, reject) {
setTimeout(function () {
if(name === 'leo') {
resolve('send success');
}else{
reject('send error');
}
});
});
}
validatePwd() {
let pwd = this.password;
return new Promise(function (resolve, reject) {
setTimeout(function () {
if(pwd === '123') {
resolve('validatePwd success');
}else{
reject('validatePwd error');
}
});
})
}
}
let user1 = new User('Joh');
user1.send()
.then(function (res) {
console.log(res);
})
.catch(function (err) {
console.log(err);
});
let user2 = new User('leo');
user2.send()
.then(function (res) {
console.log(res);
})
.catch(function (err) {
console.log(err);
});
let user3 = new User('leo', '123');
user3.validatePwd()
.then(function (res) {
return user3.validatePwd();
})
.then(function (res) {
console.log(res);
})
.catch(function(error) {
console.log(error);
});
let user4 = new User('leo', '1234');
user4.validatePwd()
.then(function (res) {
return user4.validatePwd();
})
.then(function (res) {
console.log(res);
})
.catch(function(error) {
console.log(error);
});上述就是小編為大家分享的ES6中怎么使用Promise對(duì)象了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站題目:ES6中怎么使用Promise對(duì)象-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://chinadenli.net/article4/dpegoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、小程序開發(fā)、關(guān)鍵詞優(yōu)化、電子商務(wù)、靜態(tài)網(wǎng)站、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容