第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JS fetch API:如何使用一個(gè)異步函數(shù)從多個(gè)文件中獲取內(nèi)容?

JS fetch API:如何使用一個(gè)異步函數(shù)從多個(gè)文件中獲取內(nèi)容?

aluckdog 2022-10-27 14:16:10
我想用一個(gè)異步函數(shù)從多個(gè)文件中獲取數(shù)據(jù)。目前我的代碼是這樣的:const fetchExternalData = async() => {   const resp1 = await fetch('file1.txt');   const resp2 = await fetch('file2.txt');   return resp1.text(); // How could I return the content from file2.txt as well?}fetchExternalData().then((response) => {  console.log(response); // Data from file1.txt  // How could I access the data from file2.txt?}這樣,我可以處理第一個(gè)文件中的數(shù)據(jù),但是我怎么能以這種方式訪問更多文件中的數(shù)據(jù)呢?希望這個(gè)問題可以理解。任何幫助將不勝感激。
查看完整描述

2 回答

?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

這是您可以使用的一種方法Promise.all:


const fetchExternalData = () => {

  return Promise.all([

    fetch("file1.txt"),

    fetch("file2.txt")

  ])

  .then(

    results => Promise.all(

      results.map(result => result.text())

    )

  )

}

然后,在調(diào)用您的fetchExternalData函數(shù)時(shí),您將獲得一個(gè)包含兩個(gè)文件中數(shù)據(jù)的項(xiàng)目數(shù)組:


fetchExternalData().then(

  (response) => {

    // [file1data, file2data]

  }

)

這是一個(gè)例子:


const fetchExternalData = () => {

  return Promise.all([

    fetch("https://jsonplaceholder.typicode.com/todos/1"),

    fetch("https://jsonplaceholder.typicode.com/todos/2")

  ]).then(results => {

    return Promise.all(results.map(result => result.json()));

  });

};


fetchExternalData()

  .then(result => {

    // console.log(result);

  })

  .catch(console.error);

或者,如果您想返回 anobject而不是 an array,您可以執(zhí)行以下操作:


const fetchExternalData = items => {

  return Promise.all(

    items.map(item =>

      fetch(`https://jsonplaceholder.typicode.com/todos/${item.id}`)

    )

  )

  .then(

    responses => Promise.all(

      responses.map(response => response.json())

    )

  )

  // use `Array.reduce` to map your responses to appropriate keys

  .then(results =>

    results.reduce((acc, result, idx) => {

      const key = items[idx].key;

      

      // use destructing assignment to add 

      // a new key/value pair to the final object

      return {

        ...acc,

        [key]: result

      };

    }, {})

  );

};


fetchExternalData([

  { id: 1, key: "todo1" }, 

  { id: 2, key: "todo2" }

])

  .then(result => {

    console.log("result", result);

    console.log('todo1', result["todo1"]);

  })

  .catch(console.error);


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

通過將其放入對(duì)象中返回多個(gè)值。像這樣:


const fetchExternalData = async() => {

   const resp1 = await fetch('file1.txt');

   const resp2 = await fetch('file2.txt');

   return ({res1: resp1.text(), res2: resp2.text()});

}


查看完整回答
反對(duì) 回復(fù) 2022-10-27
  • 2 回答
  • 0 關(guān)注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)