如何在一个函数中同时使用来自两个不同端点的json数据?

wfveoks0  于 2021-09-23  发布在  Java
关注(0)|答案(4)|浏览(185)

所以我想从两个不同的端点提取数据,同时将它们发送到一个函数,作为该函数的参数。

function otherFunc(jsonData1, jsondata2){
//do stuff
}

function getJsonData(url1,url2){
fetch(url1);
fetch(url2);
.then((response) => response.json())
.then((data) => otherFunc(data));
}
getJsonData(someUrl1, someOtherUrl2);

因此,我知道如何向函数发送一组数据,知道如何发出多个get请求,但不知道如何将两组jsondata作为参数发送到同一个函数。
提前谢谢

uhry853o

uhry853o1#

使用async/await更清楚一些

function otherFunc(jsonData1, jsonData2) {
  //do stuff
}

async function getJsonData(url1, url2) {
  const res1 = await fetch(url1);
  const res2 = await fetch(url2);

  const json1 = await res1.json();
  const json2 = await res2.json();

  otherFunc(json1, json2);
}

getJsonData(someUrl1, someOtherUrl2);

交替使用 Promise.all() ```
function otherFunc(jsonData1, jsonData2) {
//do stuff
}

async function getJsonData(url1, url2) {
const resArr = await Promise.all(
[url1, url2].map(url => fetch(url))
);

const [json1, json2] = await Promise.all(
resArr.map(res => res.json())
);

otherFunc(json1, json2)
}

getJsonData(someUrl1, someOtherUrl2);

function otherFunc(jsonData1, jsonData2) {
//do stuff
}

function getJsonData(url1, url2) {
Promise.all([
fetch(url1),
fetch(url2)
]).then(responses => //map the returned promises to resolve json
Promise.all(responses.map(res => res.json()))
).then(([json1, json2]) => // destructure resolved json
otherFunc(json1, json2)
).catch(error =>
// if there's an error, log it
console.log(error));
}

getJsonData(someUrl1, someOtherUrl2);

sauutmhj

sauutmhj2#

function otherFunc(jsonData1, jsondata2){
//do stuff
}

function getJsonData(url1,url2){
    fetch(url1).then(res1 => {
        fetch(url2).then(res2 => {
            // here you have both responses ready
            otherFunc(res1.json(), res2.json());
        })
    })
}
getJsonData(someUrl1, someOtherUrl2);

使用异步等待的解决方案2:

async function getData(url = '') {
  const response = await fetch(url);
  return response.json();
}

function otherFunc(jsonData1, jsondata2){
    //do stuff
}

async function getJsonData(url1,url2){
    const res1 = await getData(url1);
    const res2 = await getData(url2);

    otherFunc(res1, res2);
}

getJsonData(someUrl1, someOtherUrl2);
gstyhher

gstyhher3#

我会将这两个URL添加到getjsondata中的一个数组中,并通过它们进行循环,将结果存储到一个数组中

function getJSONData(url1,url2){
  const urlArr = [url1, url2]
  const resultsArr = []
  for(let x = 0; x < arr.length){
    fetch(arr[x])
  }

  return resultsArr
}
tyg4sfes

tyg4sfes4#

受这种等待多个承诺完成的启发,也许这可以奏效,避免任何级联等待。

function otherFunc(jsonData1, jsondata2){
//do stuff
}

function getJsonData(url1,url2){
    var json1, json2;

    const promise1 = fetch(url1).then(res1 => { json1 = res1.json() });
    const promise2 = = fetch(url2).then(res1 => { json2 = res1.json() });

    Promise.all([promise1, promise2]).then((values) => {
      otherFunc(values[0], values[1]);

      //Or if order of parmeter matter

      otherFunc(json1, json2);
    });
}

getJsonData(someUrl1, someOtherUrl2);

相关问题