ES6中異步對象Promise用法詳解
本文實例講述了ES6中異步對象Promise用法。分享給大家供大家參考,具體如下:
回憶一下ES5中的怎么使用異步方法
// es5中的異步回調(diào)
let ajax = function(callback){
console.log('執(zhí)行') // 執(zhí)行
setTimeout(() => {
callback&&callback.call();
}, 1000)
};
ajax(function(){
console.log('hello') // 1s后打印hello
});
使用ES6的Promise的方法:
let ajax = function(){
console.log('執(zhí)行2'); // 執(zhí)行2
return new Promise((resolve,reject)=>{
setTimeout(() => {
resolve() // 執(zhí)行下一步操作, reject // 中斷當前的操作
},5000)
})
}
ajax().then(()=>{
console.log('promise','timeout2') // 5s后打印 promise timeout2
})
連續(xù)使用Promise對象:
let ajax = function(){
console.log('執(zhí)行3');
return new Promise((resolve,reject)=>{
setTimeout(() => {
resolve();
},5000)
})
}
ajax().then(function(){
return new Promise(function(resolve,reject){
setTimeout(() => {
resolve()
}, 2000)
})
}).then(function(){
console.log('timeouk3') // 7s后打印timeouk3
})
Promise捕獲錯誤的catch( )
// catch 捕獲錯誤
let ajax = function(num){
console.log('執(zhí)行4');
return new Promise(function(resolve,reject){
if(num>=5){
resolve();
}else{
throw new Error('傳入的數(shù)字比5小')
}
})
}
ajax(4).then(function(){
console.log('log',4);
}).catch(function(err){
console.log('catch',err); // catch Error: 傳入的數(shù)字比5小
})
Promise的兩種高級用法
1.Promise.all( )
function loadImg(src){
return new Promise((resolve,reject)=>{
let img = document.createElement('img');
img.src = src;
img.onload = function(){
resolve(img);
}
img.onerror = function(err){
reject(err);
}
})
}
// 對所有的圖片進行遍歷
function showImgs(imgs){
console.log(imgs);
imgs.forEach(function(img){
document.body.appendChild(img);
})
}
// promise.all()的用法
Promise.all([
loadImg('http://yanshi.sucaihuo.com/modals/0/23/small.jpg'),
loadImg('http://yanshi.sucaihuo.com/modals/0/20/small.jpg'),
loadImg('http://yanshi.sucaihuo.com/modals/0/2/small.jpg')
]).then(showImgs)
/**解析:
把多個promise實例當做一個promise實例,當所有的promise實例發(fā)生變化后
新的promise實例才會發(fā)生變化
*/
2.Promise.race( )
// Promise 先加載 有一個圖片加載完就先添加到頁面
function loadImg(src){
return new Promise((resolve,reject)=>{
let img = document.createElement('img');
img.src = src;
img.onload = function(){
resolve(img);
}
img.onerror = function(err){
reject(err);
}
})
}
function showImgs(img){
let p = document.createElement('p');
p.appendChild(img);
document.body.appendChild(p);
}
// promise.race()的用法 只要有一個網(wǎng)速好的加載完,其他的就不加載了
Promise.race([
loadImg('http://yanshi.sucaihuo.com/modals/0/23/small.jpg'),
loadImg('http://yanshi.sucaihuo.com/modals/0/20/small.jpg'),
loadImg('http://yanshi.sucaihuo.com/modals/0/2/small.jpg')
]).then(showImgs)
}
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關于JavaScript相關內(nèi)容可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學運算用法總結(jié)》
希望本文所述對大家JavaScript程序設計有所幫助。
相關文章
window.location的重寫及判斷l(xiāng)ocation是否被重寫
這篇文章主要介紹了window.location的重寫及判斷l(xiāng)ocation是否被重寫,需要的朋友可以參考下2014-09-09
原生態(tài)js,鼠標按下后,經(jīng)過了那些單元格的簡單實例
下面小編就為大家?guī)硪黄鷳B(tài)js,鼠標按下后,經(jīng)過了那些單元格的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
JavaScript中的apply()方法和call()方法使用介紹
我們發(fā)現(xiàn)apply()和call()的真正用武之地是能夠擴充函數(shù)賴以運行的作用域,如果我們想用傳統(tǒng)的方法實現(xiàn)2012-07-07
javascript實現(xiàn)uploadify上傳格式以及個數(shù)限制
這篇文章主要介紹了javascript如何限制uploadify上傳格式以及個數(shù)的實現(xiàn)方法,感興趣的小伙伴們可以參考一下2015-11-11
調(diào)用innerHTML之后onclick失效問題的解決方法
調(diào)用innerHTML之后,onclick失效了,這也是在意料之中的,因為innerHTML是以文本形式插入的button,所以無法識別onclick事件2014-01-01

