【问题标题】:Return dependent promises sequentially inside loop在循环内按顺序返回依赖的 Promise
【发布时间】:2018-11-02 21:22:56
【问题描述】:

我正在开发 shopify 集成。 我们收到一个数组项,然后循环遍历它们并为它们添加一个新模型(func1),然后我需要使用第一个结果并将其添加到计划中(func2)。

我需要此函数按顺序运行,因为我将结果添加到计划中,如果我有两个相同日期的结果并且它们在数据库中尚不存在,如果它们并行运行它会创建 2 个单独的数据库中的条目而不是具有两个值的条目。

我需要返回的方式是func1,func2,func1,func2....

但此刻正在返回 func1, func1...func2, func2...

这是我需要完成的一个简化示例。

 const func2 = () => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          return console.log('func2');
        }, 3000);
      });
    };

const func1 = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('Func1');
      func2();
    }, 1000);
  });
};

const array = [1, 2, 3, 4, 5];

const test = () => {
  array.map(x => {
    func1();
  });
};
test();

如果有不清楚的地方,请告诉我。 谢谢

【问题讨论】:

  • 您无法从 setTimeout 返回。你有承诺,你不调用resolve?

标签: javascript arrays


【解决方案1】:

您可以使用 async/await 和 for 循环来创建类似同步的迭代。并在你的 func1 中再次使用它以重新爱

const func2 = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
       console.log('func2');
       resolve();
    }, 3000);
  });
};

const func1 =  () => {
  return new Promise( (resolve, reject) => {
  setTimeout(async () => {
      console.log('Func1');
      await func2();
      resolve();
    }, 1000);
  });
};

const array = [1, 2, 3, 4, 5];

const test = async () => {
  for(let i=0;i<array.length;i++){
    await func1();

  }
};
test();

【讨论】:

  • func2().then(resolve) 如果您不希望对setTimeout 的回调为async,也可以使用。
【解决方案2】:

这是使用traverse 函数的理想场所:

const traverse = (xs, f) => xs.reduce((promise, x) =>
    promise.then(ys => f(x).then(y => Promise.resolve(ys.concat([y])))),
    Promise.resolve([]));

const times2 = x => new Promise(resolve => setTimeout(() => {
    console.log("times2", x);
    resolve(2 * x);
}, 1000));

const minus1 = x => new Promise(resolve => setTimeout(() => {
    console.log("minus1", x);
    resolve(x - 1);
}, 1000));

const xs = [1,2,3,4,5];

const ys = traverse(xs, x => times2(x).then(minus1));

ys.then(console.log); // [1,3,5,7,9]

希望对您有所帮助。

【讨论】:

    【解决方案3】:

    const func2 = async (modalValue) => {
      let result = modalValue*5;
      console.log(`Function2 result: ${result}`)
      return result; 
    };
    
    
    async function getModalValue(i){
      // rest of your Code
      return { modal: i*2}
    }
    
    
    const func1 =  async (item) => { 
       let {modal} = await getModalValue(item);
       console.log(`Function1 modal: ${modal}`)
       await func2(modal);
    };
    
    const array = [1, 2, 3, 4, 5];
    
    const test = async () => {
      for(let i=0;i<array.length;i++){
        await func1(array[i]);
      }
    };
    
    test().then((resp)=> {console.log("Completed")})
     .catch((err)=>{console.log("failure")})

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 2016-07-11
      相关资源
      最近更新 更多