最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript Promise 用法

 更新時間:2016年06月14日 12:00:57   投稿:lijiao  
“Promises”漸漸成為JavaScript里最重要的一部分,大量的新API都開始promise原理實現(xiàn)。這篇文章主要為大家詳細介紹了JavaScript Promise 用法,感興趣的小伙伴們可以參考一下

同步編程通常來說易于調(diào)試和維護,然而,異步編程通常能獲得更好的性能和更大的靈活性。異步的最大特點是無需等待。“Promises”漸漸成為JavaScript里最重要的一部分,大量的新API都開始promise原理實現(xiàn)。下面讓我們看一下什么是promise,以及它的API和用法!

Promises現(xiàn)狀

XMLHttpRequest API是異步的,但它沒有使用promise API。但有很多原生的 javascript API 使用了promise:

*Battery API
*fetch API (XHR的替代品)
*ServiceWorker API

Promises將來只會變得越來越流行、普遍,非常重要,所有的前端開發(fā)人員都將用到它。另一個值得注意的是,Node.js是基于Promises的平臺(很顯然,Promise是它的一個核心特征)。

Promises的用法比你想象的要簡單——如果你以前喜歡使用setTimeout來控制異步任務(wù)的話!

Promise基本用法

new Promise()構(gòu)造器可以用在傳統(tǒng)的異步任務(wù)中,就像以前 setTimeout 和 XMLHttpRequest 的用法一樣。一個新的 Promise 使用 new 關(guān)鍵字生成,同時,這個 Promises 提供了 resolve 和 reject 函數(shù)讓我們執(zhí)行回調(diào)操作:

var p = new Promise(function(resolve, reject) {
 
 // Do an async task async task and then...

 if(/* good condition */) {
 resolve('Success!');
 }
 else {
 reject('Failure!');
 }
});

p.then(function() { 
 /* do something with the result */
}).catch(function() {
 /* error */
})

程序員可以手動的在回調(diào)函數(shù)內(nèi)部根據(jù)執(zhí)行情況調(diào)用 resolve 和 reject 函數(shù)。下面是一個比較具有現(xiàn)實意義的例子,它將一個 XMLHttpRequest 調(diào)用轉(zhuǎn)換為 基于 Promises 的任務(wù):

// From Jake Archibald's Promises and Back:
// http://www.html5rocks.com/en/tutorials/es6/promises/#toc-promisifying-xmlhttprequest

function get(url) {
 // Return a new promise.
 return new Promise(function(resolve, reject) {
  // Do the usual XHR stuff
  var req = new XMLHttpRequest();
  req.open('GET', url);

  req.onload = function() {
   // This is called even on 404 etc
   // so check the status
   if (req.status == 200) {
    // Resolve the promise with the response text
    resolve(req.response);
   }
   else {
    // Otherwise reject with the status text
    // which will hopefully be a meaningful error
    reject(Error(req.statusText));
   }
  };

  // Handle network errors
  req.onerror = function() {
   reject(Error("Network Error"));
  };

  // Make the request
  req.send();
 });
}

// Use it!
get('story.json').then(function(response) {
 console.log("Success!", response);
}, function(error) {
 console.error("Failed!", error);
});

Promise.resolve() 和 Promise.reject() 可以直接被調(diào)用。有時候,當(dāng)判斷出 promise 并不需要真正執(zhí)行時,我們并不需要 使用 new 創(chuàng)建 Promise 對象,而是可以直接調(diào)用 Promise.resolve() 和 Promise.reject()。比如:

var userCache = {};

function getUserDetail(username) {
 // In both cases, cached or not, a promise will be returned

 if (userCache[username]) {
  // Return a promise without the "new" keyword
  return Promise.resolve(userCache[username]);
 }

 // Use the fetch API to get the information
 // fetch returns a promise
 return fetch('users/' + username + '.json')
  .then(function(result) {
   userCache[username] = result;
   return result;
  })
  .catch(function() {
   throw new Error('Could not find user: ' + username);
  });
}

因為 promise 肯定會返回,所以,我們可以使用 then 和 catch 方法處理返回值!

then 方法

所有的 promise 對象實例里都有一個 then 方法,它是用來跟這個 promise 進行交互的。首先,then 方法會缺省調(diào)用 resolve() 函數(shù):

new Promise(function(resolve, reject) {
 // A mock async action using setTimeout
 setTimeout(function() { resolve(10); }, 3000);
})
.then(function(result) {
 console.log(result);
});

// From the console:
// 10

then 回調(diào)動作的觸發(fā)時機是 promise 被執(zhí)行完。我們還可以串聯(lián) then 方法執(zhí)行回調(diào)操作:

new Promise(function(resolve, reject) { 
 // A mock async action using setTimeout
 setTimeout(function() { resolve(10); }, 3000);
})
.then(function(num) { console.log('first then: ', num); return num * 2; })
.then(function(num) { console.log('second then: ', num); return num * 2; })
.then(function(num) { console.log('last then: ', num);});

// From the console:
// first then: 10
// second then: 20
// last then: 40

你會發(fā)現(xiàn),每次 then 調(diào)用都會以之前的 then 調(diào)用的返回值為參數(shù)。

如果一個 promise 已經(jīng)執(zhí)行完成,單 then 被再次調(diào)用時,回調(diào)動作將會被再次執(zhí)行。而如果這個 promise 里執(zhí)行的是reject 回調(diào)函數(shù),這是再調(diào)用 then 方法,回調(diào)函數(shù)將不會被執(zhí)行。

catch 方法

catch 當(dāng)一個 promise 被拒絕(reject)時,catch 方法會被執(zhí)行:

new Promise(function(resolve, reject) {
 // A mock async action using setTimeout
 setTimeout(function() { reject('Done!'); }, 3000);
})
.then(function(e) { console.log('done', e); })
.catch(function(e) { console.log('catch: ', e); });

// From the console:
// 'catch: Done!'

通常我們在 reject 方法里處理執(zhí)行失敗的結(jié)果,而在catch 里執(zhí)行異常結(jié)果:

reject(Error('Data could not be found'));

Promise.all 方法

在我們的異步調(diào)用時經(jīng)常有這樣一種場景:我們需要同時調(diào)用多個異步操作,但希望只有等所有的操作都完成后,我們才去執(zhí)行響應(yīng)操作——這就是 Promise.all 的作用。 Promise.all 方法可以接收多個 promise 作為參數(shù),以數(shù)組的形式,當(dāng)這些 promise 都成功執(zhí)行完成后才調(diào)用回調(diào)函數(shù)。

Promise.all([promise1, promise2]).then(function(results) {
 // Both promises resolved
})
.catch(function(error) {
 // One or more promises was rejected
});

一個很好的能演示 Promise.all 用法的例子是,執(zhí)行多個 AJAX 操作(通過 fetch) 調(diào)用:

var request1 = fetch('/users.json');
var request2 = fetch('/articles.json');

Promise.all([request1, request2]).then(function(results) {
 // Both promises done!
});

我們還可將fetch和電池狀態(tài)API混合一起執(zhí)行,因為它們返回的都是 promise:

Promise.all([fetch('/users.json'), navigator.getBattery()]).then(function(results) {
 // Both promises done!
});

一旦 promise 里調(diào)用了reject函數(shù),也就是執(zhí)行被拒絕了,沒有能夠正常完成,情況會有些復(fù)雜。一旦 promise 被拒絕,catch 方法會捕捉到首個被執(zhí)行的reject函數(shù):

var req1 = new Promise(function(resolve, reject) { 
 // A mock async action using setTimeout
 setTimeout(function() { resolve('First!'); }, 4000);
});
var req2 = new Promise(function(resolve, reject) { 
 // A mock async action using setTimeout
 setTimeout(function() { reject('Second!'); }, 3000);
});
Promise.all([req1, req2]).then(function(results) {
 console.log('Then: ', one);
}).catch(function(err) {
 console.log('Catch: ', err);
});

// From the console:
// Catch: Second!

Promise.all 是非常重要的接口,將會在很多新誕生的 promise API中扮演重要的作用。

Promise.race

Promise.race 是一個有趣的函數(shù)——它不是等待所有的 promise 被resolve 或 reject,而是在所有的 promise 中只要有一個執(zhí)行結(jié)束,它就會觸發(fā):

var req1 = new Promise(function(resolve, reject) { 
 // A mock async action using setTimeout
 setTimeout(function() { resolve('First!'); }, 8000);
});
var req2 = new Promise(function(resolve, reject) { 
 // A mock async action using setTimeout
 setTimeout(function() { resolve('Second!'); }, 3000);
});
Promise.race([req1, req2]).then(function(one) {
 console.log('Then: ', one);
}).catch(function(one, two) {
 console.log('Catch: ', one);
});

// From the console:
// Then: Second!

一個有用的場景是,從多個鏡像服務(wù)器下載資源,一旦有一個返回,其它的返回也就不用處理了。

學(xué)會使用 Promises

Promises在過去幾年是一個非?;鸨脑掝},它甚至從JavaScript里抽離出來變成了一個語言架構(gòu)。相信很快我們將見到有愈來愈多的JavaScript API將使用以promise為基礎(chǔ)的模式。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JavaScript單一職責(zé)原則深入分析

    JavaScript單一職責(zé)原則深入分析

    這篇文章主要介紹了理解JavaScript單一職責(zé)原則,單一職責(zé)原則(SRP:Single?responsibility?principle)又稱單一功能原則,面向?qū)ο笪鍌€基本原則(SOLID)之一,下文更多相關(guān)介紹感興趣的小伙伴可以參考一下
    2022-08-08
  • 簡單聊聊JavaScript的事件循環(huán)機制

    簡單聊聊JavaScript的事件循環(huán)機制

    前端開發(fā)的童鞋應(yīng)該都知道,JavaScript是一門單線程的腳本語言,這就意味著JavaScript 代碼在執(zhí)行的時候,只有一個主線程來執(zhí)行所有的任務(wù),同一個時間只能做同一件事情,這篇文章主要給大家介紹了關(guān)于JavaScript事件循環(huán)機制的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • js給table賦值的實例代碼

    js給table賦值的實例代碼

    下面小編就為大家?guī)硪黄猨s給table賦值的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • nodejs入門詳解(多篇文章結(jié)合)

    nodejs入門詳解(多篇文章結(jié)合)

    Node.js是一套用來編寫高性能網(wǎng)絡(luò)服務(wù)器的JavaScript工具包,一系列的變化由此開始。比較獨特的是,Node.js會假設(shè)你是在POSIX環(huán)境下運行它Linux 或 Mac OS X
    2012-03-03
  • 只要1K 純JS腳本送你一朵3D紅色玫瑰

    只要1K 純JS腳本送你一朵3D紅色玫瑰

    這篇文章主要介紹了只要1K大小,純JS腳本送你一朵3D玫瑰花,屬于你的情人節(jié)專屬玫瑰,感興趣的小伙伴們可以參考一下
    2016-08-08
  • JS實現(xiàn)中英文混合文字溢出友好截取功能

    JS實現(xiàn)中英文混合文字溢出友好截取功能

    這篇文章主要介紹了JS實現(xiàn)中英文混合文字溢出友好截取功能,通常會用到j(luò)s的 substr 或者 substring方法, 以及 字符串的length屬性。需要的朋友可以參考下
    2018-08-08
  • js 通過cookie實現(xiàn)刷新不變化樹形菜單

    js 通過cookie實現(xiàn)刷新不變化樹形菜單

    通過設(shè)置cookie來保存樹形菜單的狀態(tài),在頁面加載時重新讀取cookie來設(shè)置菜單
    2014-10-10
  • 理解JavaScript事件對象

    理解JavaScript事件對象

    這篇文章主要為大家介紹了JavaScript事件對象,了解JavaScript事件
    2016-01-01
  • table insertRow、deleteRow定義和用法總結(jié)

    table insertRow、deleteRow定義和用法總結(jié)

    這篇文章主要對table insertRow、deleteRow定義和用法做下總結(jié),需要的朋友可以參考下
    2014-05-05
  • SyntaxHighlighter代碼加色使用方法

    SyntaxHighlighter代碼加色使用方法

    原名:SyntaxHighlighter,是一款用于web頁面的代碼著色工具,可以用來著色多種語言,可以是HTML,CSS,Javascript,還可以是C,JAVA等編程語言。最早見于Yahoo的YUI,當(dāng)時還屬于自由軟件,最近打開官方網(wǎng)站發(fā)現(xiàn)已被goolge收編。
    2008-09-09

最新評論

沁阳市| 墨江| 尤溪县| 灯塔市| 略阳县| 东丰县| 陇南市| 鱼台县| 历史| 伽师县| 晴隆县| 平泉县| 尉氏县| 新巴尔虎左旗| 渑池县| 江陵县| 贡嘎县| 东乡族自治县| 金湖县| 巴林右旗| 揭西县| 敖汉旗| 景洪市| 缙云县| 辛集市| 买车| 东兴市| 泸州市| 缙云县| 松潘县| 伊春市| 应城市| 西藏| 元阳县| 绥芬河市| 梅州市| 浮山县| 长岛县| 武冈市| 瓮安县| 上虞市|