简体   繁体   English

我在 Javascript 中链接异步代码时遇到问题

[英]I am facing problem chaining the async code in Javascript

I am trying to execute allCountryData and return a promise its working fine but after allCountryData is done executing I want to perform a operation on that returned data / or allCountryDataArray and store the highest values in arrayOfHighestCases我正在尝试执行 allCountryData 并返回 promise 它工作正常,但在 allCountryData 完成执行后,我想对返回的数据/或 allCountryDataArray 执行操作并将最高值存储在 arrayOfHighestCases

Note I can't chain the other login in allCountryData.注意我不能在 allCountryData 中链接其他登录。

Please help let me know if you need any more details如果您需要更多详细信息,请帮助让我知道


export const allCountryDataArray = [];
export const arrayOfHighestCases = [];

const allCountryData = async () => {
  sendHTTP()
    .then((res) => {
      return res.response;
    })
    .then((res) => {
      allCountryDataArray.push(...res);
      return allCountryDataArray;
    });
  return await allCountryDataArray;

  // Highest Cases
};

The code is below is not working下面的代码不起作用


const highestCasesData = async () => {
  // const allCountryDataArrayy = await allCountryData();
  // allCountryData()
  // .then((data) => {
  //   console.log(arrayOfHighestCases[0]);
  // })
  // .then((res) => {
  const np = new Promise((res, rej) => {
    res(allCountryData());
  });

  return np.then((res) => {
    console.log(res);
    const arrayofHigh = allCountryDataArray.sort((a, b) => {
      if (a.cases.total < b.cases.total) {
        return 1;
      } else if (a.cases.total > b.cases.total) {
        return -1;
      } else {
        return 0;
      }
    });
    console.log(arrayofHigh);
    const slicedArray = arrayofHigh.slice(0, 6);
    for (const eachHighCase of slicedArray) {
      arrayOfHighestCases.push(eachHighCase);
    }
    console.log(arrayOfHighestCases);
    return arrayOfHighestCases;
  });

  // });
};
highestCasesData();

Filling global arrays with async data is a way into timing conflicts.用异步数据填充全局 arrays 是导致时序冲突的一种方式。 Bugs where the data ain't there, except when you look it is there and yet another question here on my SO about "Why can't my code access data? When I check in the console everything looks fine, but my code ain't working."数据不存在的错误,除非你看它在那里,还有一个关于我的 SO 上的另一个问题是“为什么我的代码不能访问数据?当我在控制台中签入时,一切看起来都很好,但我的代码是”不工作。”

If you want to store something, store Promises of these arrays or memoize the functions.如果你想存储一些东西,存储这些arrays的 Promises 或者记忆函数。

const allCountryData = async () => {
  const res = await sendHTTP();
  return res.response;
};

const highestCasesData = async () => {
  const allCountryDataArray = await allCountryData();

  return allCountryDataArray
    .slice()  // make a copy, don't mutate the original array
    .sort((a, b) => b.cases.total - a.cases.total)  // sort it by total cases DESC
    .slice(0, 6); // take the first 6 items with the highest total cases
}

This is working please let me know if I can make some more improvements这是有效的,请让我知道我是否可以进行更多改进

const allCountryData = async () => {
  return sendHTTP()
    .then((res) => {
      return res.response;
    })
    .then((res) => {
      allCountryDataArray.push(...res);
      return allCountryDataArray;
    });

  // Highest Cases
};

const highestCasesData = async () => {

  return allCountryData().then((res) => {
    console.log(res);
    const arrayofHigh = allCountryDataArray.sort((a, b) => {
      if (a.cases.total < b.cases.total) {
        return 1;
      } else if (a.cases.total > b.cases.total) {
        return -1;
      } else {
        return 0;
      }
    });
    console.log(arrayofHigh);
    const slicedArray = arrayofHigh.slice(0, 6);
    for (const eachHighCase of slicedArray) {
      arrayOfHighestCases.push(eachHighCase);
    }
    console.log(arrayOfHighestCases);
    return arrayOfHighestCases;
  });

};
highestCasesData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在 JavaScript/ReactJS 中遇到问题 - I am facing a problem in JavaScript/ReactJS "我在 React Native 中遇到了关于异步存储的问题" - i am facing a problem regarding async storage in react native 我在使用 javascript 数组时遇到了一个困难且关键的问题? - I am facing a difficult and critical problem with javascript array? 我面临问题引导表响应 - I am facing problem bootstrap table responsive 我面临安装 react-currency-format 的问题? - I am facing the problem to install react-currency-format? 我在将服务数据推送到ANGULAR中的数组时遇到问题 - I am facing problem in pushing my service data to the Array in ANGULAR 为什么我在语音RSS API上遇到一些问题 - Why I am facing some problem with voice RSS api 我遇到了`class` 和`id` 属性的问题。使用`id` 和`<img> ` 代码正常工作。但是使用 `class` 它不起作用 - I am facing problem with `class` and `id` properties.Using `id` with `<img>` code work properly.But with `class` it's not working 链接异步方法调用 - javascript - chaining async method calls - javascript 用于链接的javascript&#39;this&#39;代码 - javascript 'this' code for chaining
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM