使用?JavaScript?Promise?讀取?Github?用戶數(shù)據(jù)
代碼如下:
// Make a request for user.json fetch('/article/promise-chaining/user.json') // Load it as json .then(response => response.json()) // Make a request to GitHub .then(user => fetch(`https://api.github.com/users/${user.name}`)) // Load the response as json .then(response => response.json()) // Show the avatar image (githubUser.avatar_url) for 3 seconds (maybe animate it) .then(githubUser => { let img = document.createElement('img'); img.src = githubUser.avatar_url; img.className = "promise-avatar-example"; document.body.append(img); setTimeout(() => img.remove(), 3000); // (*) });
這里的語義比較清楚,每一個 then 調(diào)用都返回一個 Promise,后續(xù)的 then 調(diào)用,必須在前一個 then 調(diào)用返回的 Promise 被 resolve 之后,才能得到執(zhí)行。
不過上述代碼有一個缺陷:
看 *
所在行的代碼:在頭像完成顯示并被移除后,如果我們想添加一些額外的處理邏輯,應(yīng)該怎么做? 例如,我們想顯示一個用于編輯該用戶或其他內(nèi)容的表單。
為了使鏈可擴(kuò)展,我們需要返回一個在頭像完成顯示時進(jìn)行 resolve 的 Promise.
代碼如下:
fetch('/article/promise-chaining/user.json') .then(response => response.json()) .then(user => fetch(`https://api.github.com/users/${user.name}`)) .then(response => response.json()) .then(githubUser => new Promise(function(resolve, reject) { // (*) let img = document.createElement('img'); img.src = githubUser.avatar_url; img.className = "promise-avatar-example"; document.body.append(img); setTimeout(() => { img.remove(); resolve(githubUser); // (**) }, 3000); })) // triggers after 3 seconds .then(githubUser => alert(`Finished showing ${githubUser.name}`));
也就是說,(*) 行中的 .then 處理程序現(xiàn)在返回新的 Promise,該 Promise 僅在 setTimeout (**) 中的 resolve(githubUser) 調(diào)用后才被解決。 鏈中的下一個 .then 將等待它。
下圖第 5 行新建的 Promise 對象,這個對象在第 13 行會 resolve,這個 resolve 操作,會觸發(fā)等待它的第 17 行的 then 方法。
作為一種好的實踐,異步操作應(yīng)始終返回一個 Promise. 這使得在它之后的計劃行動成為可能;即使我們現(xiàn)在不打算擴(kuò)展鏈,我們以后也可能需要它。
最后我們對代碼進(jìn)行重構(gòu)。
function loadJson(url) { return fetch(url) .then(response => response.json()); }
以上的函數(shù)返回一個 Promise,當(dāng) response 的 json 數(shù)據(jù)可用時,這個 promise 后注冊的 .then
函數(shù)就會觸發(fā)。
看其消費代碼:
26 行 then 里的箭頭函數(shù)觸發(fā)時,user 就是 25 行 user.json 數(shù)據(jù)被反序列化之后形成的 JSON 對象。
function loadGithubUser(name) { return loadJson(`https://api.github.com/users/${name}`); }
只是對 loadJson 的一層封裝,讓調(diào)用者不需要知道 Github user api 具體的 endpoint.
function showAvatar(githubUser) { return new Promise(function(resolve, reject) { let img = document.createElement('img'); img.src = githubUser.avatar_url; img.className = "promise-avatar-example"; document.body.append(img); setTimeout(() => { img.remove(); resolve(githubUser); }, 3000); }); }
返回一個 Promise,在其 executor 里書寫業(yè)務(wù)邏輯,并通過 resolve(githubUser) 將 Promise 狀態(tài)設(shè)置為 fulfilled,方便將來的擴(kuò)展。
最后的完整代碼:
// Use them: loadJson('/article/promise-chaining/user.json') .then(user => loadGithubUser(user.name)) .then(showAvatar) .then(githubUser => alert(`Finished showing ${githubUser.name}`)); // ...
總結(jié):
如果 .then(或 catch/finally,無關(guān)緊要)處理程序返回一個 Promise,則 Promise 鏈的其余部分會一直等待,直到這個 pending 的 Promise 被 resolve. 當(dāng) Promise 內(nèi)部的 executor 有數(shù)據(jù)被 resolve 調(diào)用時,resolve 輸入的數(shù)據(jù)(或錯誤)會被進(jìn)一步傳遞到 Promise chain 里的其他 Promise.then 中去。
到此這篇關(guān)于使用 JavaScript Promise 讀取 Github 用戶數(shù)據(jù)的文章就介紹到這了,更多相關(guān) JavaScript Promise 讀取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉樹的查找算法示例
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉樹的查找算法,結(jié)合具體實例形式分析了javascript針對二叉樹節(jié)點最小值、最大值的相關(guān)查找操作實現(xiàn)技巧,需要的朋友可以參考下2017-04-04一文搞懂JSON(JavaScript Object Notation)
Json 有兩種基本的結(jié)構(gòu),即 Json對象 和 Json 數(shù)組。通過 Json 對象和 Json 數(shù)組這兩種結(jié)構(gòu)的組合可以表示各種復(fù)雜的結(jié)構(gòu),今天通過本文給大家介紹JavaScript Object Notation的基本知識,感興趣的朋友一起看看吧2021-10-10JavaScript常用基礎(chǔ)知識強(qiáng)化學(xué)習(xí)
這篇文章主要介紹了JavaScript常用基礎(chǔ)知識強(qiáng)化學(xué)習(xí),需要的朋友可以參考下2015-12-12Google Suggest ;-) 基于js的動態(tài)下拉菜單
Google Suggest ;-) 基于js的動態(tài)下拉菜單...2006-10-10JS實現(xiàn)的base64加密、md5加密及sha1加密詳解
這篇文章主要介紹了JS實現(xiàn)的base64加密、md5加密及sha1加密的方法,結(jié)合實例形式詳細(xì)分析了JavaScript各種常見加密方法與實現(xiàn)技巧,需要的朋友可以參考下2016-04-04