在 promise 的 then() 内中断 for 循环 [英] Break for loop inside then() of a promise

查看:100
本文介绍了在 promise 的 then() 内中断 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的情况,我想在收到 Rx Promise 的结果并进行一些检查后中断 for 循环.我所拥有的是以下内容:

function getDrift(groups) {无功漂移 = {};组.forEach(功能(组){if(group.type === '东西') {for(var i = 0; i < group.entries.length; i++) {fetchEntry(group.entries[i].id).then(功能(条目){if(entry.type === 'someType'){漂移[entry._id] = getCoordinates(entry);//休息;}});}}});返回漂移;}

其中 fetchEntry 返回基于 id 的 mongodb 文档的 Promise.如果满足 if 检查,我想中断当前 group.entries 上的循环并继续下一组.

这可能吗?

谢谢

编辑:根据要求,组对象如下所示:

<预><代码>[{类型:'写作',条目:[{id:someId",名称:someName"},{id:someId2",名称:someName2"}]},{类型:'阅读',条目:[{id:someId3",名称:someName3"},{id:someId4",名称:someName4"}]}]

解决方案:我最终使用了@MikeC 的建议和递归和回调来返回所需的值.谢谢大家!

解决方案

这是不可能的,因为 Promises 是异步的,这意味着 then 在所有其他同步代码完成之前不会执行.

如果您不想根据某些条件处理所有这些,我建议您创建一个函数,如果您想继续调用该函数.

(function process(index) {如果(索引> = group.entries.length){返回;}fetchEntry(group.entries[index]).then(功能(条目){if(entry.type === 'someType'){漂移[entry._id] = getCoordinates(entry);//不再调用该函数} 别的 {进程(索引 + 1);}});})(0);

I'm having a weird situation where I want to break a for loop after I have received the result of an Rx Promise and done some checks. What I have is the following:

function getDrift(groups) {
    var drift = {};
    groups.forEach(function(group) {
        if(group.type === 'something') {
            for(var i = 0; i < group.entries.length; i++) {
                fetchEntry(group.entries[i].id)
                 .then(function(entry) {
                     if(entry.type === 'someType'){
                         drift[entry._id] = getCoordinates(entry);
                         // break;
                     }
                 });
            }
        }
    });
    return drift;
}

where fetchEntry is returning a Promise of a mongodb document based on an id. If the if check is satisfied, I want to break the loop on the current group.entries and continue on to the next group.

Is that possible?

Thanks

EDIT: As requested, the groups object looks like this:

[
    {
        type: 'writing',
        entries: [{id: "someId", name: "someName"}, {id: "someId2", name: "someName2"}]
    },
    {
        type: 'reading',
        entries: [{id: "someId3", name: "someName3"}, {id: "someId4", name: "someName4"}]
    }
]

SOLUTION: I ended up using @MikeC 's suggestion with recursion and a callback to return the needed value. Thank you all!

解决方案

That isn't possible because Promises are asynchronous which means then won't execute until all other synchronous code completes.

If you don't want to process all of them based on some condition, I would suggest creating a function which you call if you want to continue.

(function process(index) {
  if (index >= group.entries.length) {
    return;
  }
  fetchEntry(group.entries[index])
    .then(function(entry) {
      if(entry.type === 'someType'){
        drift[entry._id] = getCoordinates(entry);
        // don't call the function again
      } else {
        process(index + 1);
      }
    });
})(0);

这篇关于在 promise 的 then() 内中断 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆