English 中文(简体)
在地图之后执行职能,完成多次React的Fricth API申请。 j)
原标题:Executing a function after Map() completes multiple Fecth API requests in React.js

Wrestling with promises here...

我的用户提出单一要求,要求世界科学知识与技术学会发出多次呼吁。 我将用户提出的每一项请求和两项REE答复作为项目储存在类似记录中:

[
  {
    call:userRequest,
    responses: [
        {
          source:source1
          response:responseText
        },
        {
          source:source2
          response:responseText
        }
      ]
  }
]

我利用地图功能来唤起LEST的信号。 一、采用一套办法处理每一应对措施 这套方法增加了每一次对“Call”阵列的反应细节。 记录阵列中的答复价值一经确定,就分配给了这个委员会。

await Promise.all(urls.map(async url =>  {
  
  fetch(url, { 
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: messageBody
  })
  .then((response) => response.json())
  .then((data) => {
    setThisCall(prev => ([ //sets thisCall
      ...prev,
      {
        source: source,
        response: data.body
      }
    ]))
  })
})).then(()=>{
  console.log("now adding to records")
  setRecords(prev => ([ //sets records
    {
      call: requestText,
      reponses: thisCall
      },
      ...prev
    ]))
  })

我尝试利用Promise.all()使地图功能完好坏,处理答复,然后用这个词来补充要求,来打下一个记录项目。

但是,最后一点是在建立这一中心之前发射的,造成记录中的空洞反应值。

tl;dr - 我如何制定《规则》?

问题回答
await Promise.all(
  urls.map((url) => {
    fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: messageBody,
    })
      .then((response) => response.json())
      .then((data) => {
        setThisCall((prev) => [
          //sets thisCall
          ...prev,
          {
            source: source,
            response: data.body,
          },
        ]);
      });
  })
);

console.log("now adding to records");
setRecords((prev) => [
  //sets records
  {
    call: requestText,
    reponses: thisCall,
  },
  ...prev,
]);




相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签