当前位置: 首页 > 面试题库 >

等待promise for循环

祁乐邦
2023-03-14
问题内容
let currentProduct;

for (let i = 0; i < products.length; i++) { 
    currentProduct = products[i];

    subscription.getAll(products[i]._id)
        .then((subs) => {
            update(subs, currentProduct);
        });
}

我正在使用bluebird,方法 getAllupdate return
promises。我如何说“等到两个承诺返回,然后更新currentProduct值”?我对JS很陌生…


问题答案:

如果可以使用async/,这将很简单await

// Make sure that this code is inside a function declared using
// the `async` keyword.
let currentProduct;

for (let i = 0; i < products.length; i++) { 
    currentProduct = products[i];

    // By using await, the code will halt here until
    // the promise resolves, then it will go to the
    // next iteration...
    await subscription.getAll(products[i]._id)
        .then((subs) => {
            // Make sure to return your promise here...
            return update(subs, currentProduct);
        });

    // You could also avoid the .then by using two awaits:
    /*
    const subs = await subscription.getAll(products[i]._id);
    await update(subs, currentProduct);
    */
}

或者,如果您只能使用简单的承诺,则可以遍历所有产品,并将每个承诺置于.then最后一个循环中。这样,仅当前一个问题解决时,它才会前进到下一个问题(即使它将首先迭代整个循环):

let currentProduct;

let promiseChain = Promise.resolve();
for (let i = 0; i < products.length; i++) { 
    currentProduct = products[i];

    // Note that there is a scoping issue here, since
    // none of the .then code runs till the loop completes,
    // you need to pass the current value of `currentProduct`
    // into the chain manually, to avoid having its value
    // changed before the .then code accesses it.

    const makeNextPromise = (currentProduct) => () => {
        // Make sure to return your promise here.
        return subscription.getAll(products[i]._id)
            .then((subs) => {
                // Make sure to return your promise here.
                return update(subs, currentProduct);
            });
    }

    // Note that we pass the value of `currentProduct` into the
    // function to avoid it changing as the loop iterates.
    promiseChain = promiseChain.then(makeNextPromise(currentProduct))
}

在第二个代码段中,循环仅建立了整个链,但没有.then立即执行内部代码。您的getAll功能要等到之前的每个功能依次解决后才能运行。



 类似资料:
  • 本文向大家介绍JavaScript 异步等待循环,包括了JavaScript 异步等待循环的使用技巧和注意事项,需要的朋友参考一下 示例 在循环中使用异步等待时,您可能会遇到其中一些问题。 如果您只是尝试在内部使用await forEach,则会抛出Unexpected token错误。 这是因为您错误地将箭头功能视为一个块。该await会在回调函数,这是不是上下文async。 解释器可以防止我们

  • 问题内容: 我想问这个问题,因为我不确定我是否正确使用了Node.js逻辑 我有一组ID,需要使用redis的get方法进行查询。在检查了某个值之后(假设我正在检查通过给定的“键”获取的对象是否具有空名称),然后将它们添加到列表中。这是我的示例代码; 但是由于Node的异步特性,正如预期的那样,如果不检查列表中的每个id,它将执行“ Goes here …”。我的需要是在检查每个id之后应用其余过

  • 我试图制作一个简单的应用程序来学习c(Visual Studio)中的一些东西。现在我正在制作一个音乐播放器,其中一个动作是在按钮点击事件中淡出音乐。 我对构建淡出部分没有问题,我做了一个while循环,每次循环运行时都将音量降低1%。此外,我还使用淡入度值更新标签。 唯一的问题是,为了减缓褪色,我正在使用线程。睡眠事件,该部分将冻结我的应用程序,并阻止使用淡入淡出值对我的文本标签进行任何更新。

  • 我有一个endpoint生成一些数据(需要一些时间),另一个endpoint可以用来获取生成的数据。我用wait调用第一个,从响应中获取id,并想调用第二个,而它的状态不是“Success”(它表示响应中准备和存储的数据),并继续我的脚本。 这是我的代码 它的工作,但我赢得了这么多的请求,我如何设置一些请求的间隔?我尝试使用await setInterval(),但是代码继续执行,没有等待结果 e

  • 我正在尝试将数据库调用移出控制器,以清理并使其可测试。当它们在控制器中时,一切都会顺利进行。我将它们移出控制器,并添加了一个异步,以确保我们等待。否则,我将调用的中的函数。现在,一旦我使用async/await,控制器中的函数就会认为没有用户,因为它没有等待。 有几个关于异步等待的SO问题,但我没有找到一个解决我的问题。我确实验证了返回了我的用户,并添加了控制台日志来显示路径。 节点猫鼬异步等待似

  • 我有一个控制器类,它需要为列表中的每个项目执行各种异步操作: 对于每个A 播放音频并等待完成 等t秒 对于A的每个子部分B: 播放音频并等待 等待t2秒 当我的方法被激发。我试着回复听众: 但最后我还是无法摆脱内部循环,这让我觉得整个想法都行不通。我已经查看了未来的界面,但这似乎也不是我想要的。 我知道我可以用一堆变量来解决这个问题,以跟踪我在状态流中的位置,但是带有一些框架的循环会更干净。谢谢: