如何在 Promise 鏈中排序和共享資料
Promise 為管理 JavaScript 中的非同步操作提供了一個強大的工具。作為其中的一部分,有必要對操作進行排序並在它們之間共享資料。讓我們解決具體問題:
使用Promises 連結HTTP 請求和資料共享
在此場景中,您使用Promises 執行一系列HTTP 請求,其中一個請求的回應應用作下一個請求的輸入。 callhttp 函數處理這些請求,但需要存取先前請求的資料以建構下一個請求。具體來說,您希望將從第一個請求獲取的 API 金鑰傳遞給後續請求。
資料共享和排序方法
有多種方法可以實現此目的:
1。鍊式 Promise:
使用 then 鍊式Promise,將中間資料作為參數傳遞:
callhttp(url1, payload) .then(firstResponse => { // Use the data from firstResponse in the next request return callhttp(url2, payload, firstResponse.apiKey); }) .then(secondResponse => { // Use the combined data from firstResponse and secondResponse in the next request return callhttp(url3, payload, firstResponse.apiKey, secondResponse.userId); });
2.更高範圍分配:
將中間結果分配給更高範圍內的變數:
var firstResponse; callhttp(url1, payload) .then(result => { firstResponse = result; return callhttp(url2, payload); }) .then(secondResponse => { // Use both firstResponse and secondResponse here });
3.累積結果:
將結果儲存在累積物件中:
var results = {}; callhttp(url1, payload) .then(result => { results.first = result; return callhttp(url2, payload); }) .then(result => { results.second = result; return callhttp(url3, payload); }) .then(() => { // Use the accumulated results in results object });
4.嵌套承諾:
Nest承諾保持對所有先前結果的訪問:
callhttp(url1, payload) .then(firstResponse => { // Use firstResponse here return callhttp(url2, payload) .then(secondResponse => { // Use both firstResponse and secondResponse here return callhttp(url3, payload); .then(thirdResponse => { // Use all three responses here }); }); });
5.使用Promise.all() 拆分:
如果某些請求可以獨立發出,請將鏈分成單獨的部分並使用Promise.all() 收集結果:
const first = callhttp(url1, payload); const second = callhttp(url2, payload); const third = callhttp(url3, payload); Promise.all([first, second, third]) .then(results => { // Use all three results here });
ES7 非同步/等待:
在ES7 中,async/await 語法簡化了Promise 結果的排序和處理過程,提供了更簡單、更易讀的程式碼:
async function httpRequests() { const firstResponse = await callhttp(url1, payload); const secondResponse = await callhttp(url2, payload, firstResponse.apiKey); const thirdResponse = await callhttp(url3, payload, firstResponse.apiKey, secondResponse.userId); // Use all three responses here } httpRequests();
以上是如何在 JavaScript 中的 Promise 之間高效排序和共享資料?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

因此,在這裡,您準備好了解所有稱為Ajax的東西。但是,到底是什麼? AJAX一詞是指用於創建動態,交互式Web內容的一系列寬鬆的技術。 Ajax一詞,最初由Jesse J創造

10款趣味橫生的jQuery遊戲插件,讓您的網站更具吸引力,提升用戶粘性!雖然Flash仍然是開發休閒網頁遊戲的最佳軟件,但jQuery也能創造出令人驚喜的效果,雖然無法與純動作Flash遊戲媲美,但在某些情況下,您也能在瀏覽器中獲得意想不到的樂趣。 jQuery井字棋遊戲 遊戲編程的“Hello world”,現在有了jQuery版本。 源碼 jQuery瘋狂填詞遊戲 這是一個填空遊戲,由於不知道單詞的上下文,可能會產生一些古怪的結果。 源碼 jQuery掃雷遊戲

本教程演示瞭如何使用jQuery創建迷人的視差背景效果。 我們將構建一個帶有分層圖像的標題橫幅,從而創造出令人驚嘆的視覺深度。 更新的插件可與JQuery 1.6.4及更高版本一起使用。 下載

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

Matter.js是一個用JavaScript編寫的2D剛體物理引擎。此庫可以幫助您輕鬆地在瀏覽器中模擬2D物理。它提供了許多功能,例如創建剛體並為其分配質量、面積或密度等物理屬性的能力。您還可以模擬不同類型的碰撞和力,例如重力摩擦力。 Matter.js支持所有主流瀏覽器。此外,它也適用於移動設備,因為它可以檢測觸摸並具有響應能力。所有這些功能都使其值得您投入時間學習如何使用該引擎,因為這樣您就可以輕鬆創建基於物理的2D遊戲或模擬。在本教程中,我將介紹此庫的基礎知識,包括其安裝和用法,並提供一

本文演示瞭如何使用jQuery和ajax自動每5秒自動刷新DIV的內容。 該示例從RSS提要中獲取並顯示了最新的博客文章以及最後的刷新時間戳。 加載圖像是選擇


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具