偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

JavaScript Promise 高級(jí)技巧:避免常見陷阱與解決方案

開發(fā) 前端
雖然 async/await 能解決許多 Promise 問題,但深入理解 Promise 機(jī)制至關(guān)重要。掌握這些陷阱及其解決方案,將幫助您編寫更健壯的異步代碼。

我曾在前文中介紹過 JavaScript Promise 的基礎(chǔ)知識(shí) 以及 如何使用 async/await 關(guān)鍵字 來簡(jiǎn)化異步代碼。本文將深入探討 JavaScript Promise 的高級(jí)應(yīng)用,分析開發(fā)者常遇到的四個(gè)陷阱及其解決方案。

陷阱一:Promise 處理器總是返回 Promise

無論是 then 還是 catch 處理器,只要返回值,該值就會(huì)被自動(dòng)包裝成 Promise(如果它本身不是 Promise)。因此,永遠(yuǎn)不需要這樣寫代碼:

firstAjaxCall.then(() => {
  return new Promise((resolve, reject) => {
    nextAjaxCall().then(() => resolve());
  });
});

由于 nextAjaxCall 本身返回 Promise,可以簡(jiǎn)化為:

firstAjaxCall.then(() => {
  return nextAjaxCall();
});

對(duì)于普通值(非 Promise),處理器會(huì)將其包裝為已解析的 Promise,因此可以繼續(xù)鏈?zhǔn)秸{(diào)用:

firstAjaxCall.then((response) => {
  return response.importantField
}).then((resolvedValue) => {
  // resolvedValue 就是上面返回的 response.importantField
  console.log(resolvedValue);
});

解決方案一:使用 Promise.resolve() 處理不確定值

當(dāng)不確定輸入值是否為 Promise 時(shí),可以使用靜態(tài)方法 Promise.resolve()。該方法會(huì)自動(dòng)處理兩種情況:

let processInput = (maybePromise) => {
  let definitelyPromise = Promise.resolve(maybePromise);
  definitelyPromise.then(doSomeWork);
};

陷阱二:.then() 只接受函數(shù)參數(shù)

常見錯(cuò)誤寫法:

let getAllArticles = () => someAjax.get('/articles');
let getArticleById = (id) => someAjax.get(`/articles/${id}`);

getAllArticles().then(getArticleById(2));

本意是先獲取所有文章,再獲取 ID 為 2 的文章,但實(shí)際上兩個(gè)請(qǐng)求會(huì)同時(shí)發(fā)起。問題在于 JavaScript 會(huì)立即執(zhí)行 getArticleById(2),而不是將其作為函數(shù)傳遞。

解決方案一:使用箭頭函數(shù)包裝

getAllArticles().then(() => getArticleById(2));

解決方案二:傳遞命名函數(shù)引用

let getArticle2 = () => getArticleById(2);
getAllArticles().then(getArticle2);

解決方案三:使用 async/await

async function getSequentially() {
  const allArticles = await getAllArticles();
  const specificArticle = await getArticleById(2);
  // 使用 specificArticle
}

陷阱三:非函數(shù)參數(shù)導(dǎo)致的意外行為

錯(cuò)誤示例:

getAllArticles().then(getArticleById(2)).then((article2) => {
  // article2 實(shí)際上是 getAllArticles() 的解析值
});

由于第一個(gè) .then() 接收的是立即執(zhí)行結(jié)果而非函數(shù),會(huì)導(dǎo)致后續(xù)處理器獲取到錯(cuò)誤的值。

解決方案一:使用帶形參的命名函數(shù)

let extractId = (article) => article.id;
getFirstArticle().then(extractId).then(getCommentsForArticleId);

解決方案二:使用 async/await

async function getArticleAndComments() {
  const article = await getFirstArticle();
  const comments = await getCommentsForArticleId(article.id);
  // 使用 comments
}

陷阱四:async/await 破壞并發(fā)性

錯(cuò)誤示例(順序執(zhí)行):

async function getMultipleUsersSequentially(userIds) {
  const users = [];
  for (const id of userIds) {
    const user = await fetchUserDataPromise(id); // 每次等待
    users.push(user);
  }
  return users;
}
// 三個(gè)請(qǐng)求需要約 4.5 秒(每個(gè) 1.5 秒)

解決方案:使用 Promise.all 實(shí)現(xiàn)并發(fā)

async function getMultipleUsersConcurrently(userIds) {
  const promises = userIds.map(id => fetchUserDataPromise(id));
  const users = await Promise.all(promises);
  return users;
}
// 三個(gè)并發(fā)請(qǐng)求只需約 1.5 秒

總結(jié)

雖然 async/await 能解決許多 Promise 問題,但深入理解 Promise 機(jī)制至關(guān)重要。掌握這些陷阱及其解決方案,將幫助您編寫更健壯的異步代碼。

原文地址:https://www.infoworld.com/article/3999603/javascript-promises-4-gotchas-and-how-to-avoid-them.html

責(zé)任編輯:武曉燕 來源: 前端小石匠
相關(guān)推薦

2016-09-06 12:05:23

SaaSSaaS平臺(tái)SaaS服務(wù)

2024-08-22 18:56:34

2024-04-10 08:24:29

2024-11-08 13:47:35

中文亂碼配置

2024-10-14 08:29:14

異步編程任務(wù)

2017-02-15 09:40:38

JavaScript分析解決

2020-09-09 10:00:41

JavaScript前端瓶頸

2010-05-12 14:18:58

Linux引導(dǎo)

2023-05-06 15:32:04

2009-11-18 16:10:00

2019-10-08 16:05:19

Redis數(shù)據(jù)庫系統(tǒng)

2010-08-11 15:17:51

瀏覽器兼容性問題

2018-11-18 16:31:14

Kubernetes監(jiān)控容器

2021-02-28 13:19:42

大數(shù)據(jù)IT數(shù)據(jù)管理

2009-06-10 22:13:55

JavaScriptExcel打印

2014-01-07 13:54:02

HadoopYARN

2018-08-02 15:09:20

PyTorch深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)

2024-05-09 15:00:38

Python編碼開發(fā)

2010-10-08 13:27:51

IE6pngJavaScript

2017-05-11 20:20:59

JavascriptPromiseWeb
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)