如何对异步承诺调用进行同步条件检查

How to do sync conditional check on async promise call

场景:recommendationsArr 是一个项目数组(其 99.9% 不为空,但由于它是外部 api 调用,我更愿意进行检查)。目的是获得 valueOnevalidItem 和更新的 recommendationsArr.
的值 有效性取决于 valueOne 的存在,所以如果 recommendationsArr[0] 具有有效的 valueOne 那么我不需要为数组的其余部分获取 api 结果。

const getContent = function (item) {
  console.log("####### Inside the getContent function #########");
  contentResponse = fetchContent(item);
  return contentResponse;
}

if (recommendationsArr.length > 0) {
  console.log("####### If Condition #########");
  recommendationsArr.find((item) => {
    getContent(item).then(function(response){
      try { // to get valueOne
          console.log("####### Try for: ######### ", term);
          valueOne = response.content.valueOne; //may or may not be present
          recommendationsArr.splice(recommendationsArr.indexOf(item), 1); // this is for styling first occurence is styled differently so thats popped out
          validItem = item;
          console.log("##### Valid item is: ", term);
          return true;
        } catch (err) {
          console.log("##### Item not valid: ", recommendationsArr.indexOf(item));
          valueOne = null;
          return false;
        }
    });
  });

  console.log("######### Just hanging out!!! #########");

  return {
    component: <Layout><ComponentName
      //pass other non-dependent props
      validItem={validItem}
      valueOne={valueOne}
      recommendationsArr={recommendationsArr}
    /></Layout>,
    title: `Page Title`,
  };
}

return null;

假设recommendationsArr = ["blue", "white", "green", "red"]; //usually the array is anywhere between 12-50 elements

发生了什么,控制台日志:

####### If Condition #########
####### getApiContent response for #########  blue
####### getApiContent response for #########  white
####### getApiContent response for #########  green
####### getApiContent response for #########  red
######### Just hanging out!!! #########
####### Try for: #########  blue
//I see data here
##### Valid item is:  blue
####### Try for: #########  white
//I see data here
##### Valid item is:  white
####### Try for: #########  green
//I see data here with valueOne missing in returned data
##### Item not valid: green
####### Try for: #########  red
//I see data here
##### Valid item is:  red

如您所见,API 请求 getContent 一直在请求所有条款,然后才到达 .then。这会导致一大堆 api 我什至不需要的请求-响应,我知道我正在尝试对异步 .then 的东西进行同步调用 try/catch 但我不知道如何做到这一点。

理想情况下,不应调用 API 除非 .then returns false,如果 try returns true - 不再有 API 请求并退出。另外,我需要访问 .then 之外的 response 来更新组件的道具 我怎样才能做到这一点?我简单看了下async library,适合这种情况吗?

感谢任何help/guidance..我一直坚持这个

在花了一些时间摆弄它之后,我认为最好不要尝试破解和使用 async-library

我现在使用 eachSeries 方法一次 运行 一个异步操作,

var asyncEachSeries = require('async/eachSeries');
.        
.        
.
if (recommendationsArr.length > 0) {
  asyncEachSeries(recommendationsArr, function (item, callback){
    getContent(item).then(function(response){
      try { 
          //do stuff
          return true;
        } catch (err) {
          //do stuff
        }
      return response;
    });
  });
}