for循环中的已链接诺言无法正确执行

deEcho

我有可链接的promise,在一个系列中都可以正常工作,但是现在我想在for loop中调用chain的这个serios,但是它不能按预期的方式工作。

请参阅我的演示插件,并在控制台中查看输出。

以下是我的链接承诺的结构。我希望所有的publicIP由返回funTwo但是我想完成funThree()然后想要获得所有publicIP据我所知,这$q.when()在Promise对象中很有价值。

但是你可以看到,console.log('pA', promiseArray);非常之前执行,console.log('res three');以及为什么successHandlerfinally之前叫什么?

在这里,我肯定会丢失一些东西,可能必须return;在适当的位置写一个,请帮助我如何在for循环中执行所有功能,并在for循环结束后返回一个可以重试的数据数组。successHandler

 MyService.funZero()
     .then(function(response) {
        console.log(response);
        var promiseArray = [];
        for(var i = 0; i < 2 ; i++) {
            console.log('I', i);
            MyService.funOne()
           .then(MyService.funTwo)
           .then(function(res2) {
                console.log('res two', res2);
                publicIP = res2.ip;
                console.log('i', publicIP);
                promiseArray.push({'ip': publicIP});
                return MyService.funThree(publicIP);
           })
           .then(function() {
                 console.log('res three');
           })
           } // for loop ends
        console.log('pA', promiseArray);
        return $q.when(promiseArray);
      })
      .then(function(res4){
          console.log('after for loop', res4);
      })
      .then(successHandler)
      .catch(errorHandler)
      .finally(final, notify);
卢卡斯(RA Lucas)

因此,我不确定MyService.funThree到底是做什么的,但是您可以通过聚合一个数组,然后将其ipArray.push({'ip': publicIP})返回给MyService.funThree然后是后续函数。ipArray如果您要寻找的是这里的问题,就不能保证订单的顺序这是该函数的中间部分:

ipArray = [];
for(var i = 0; i < 2 ; i++) {
  console.log('I', i);
  var promise = MyService.funOne()
    .then(MyService.funTwo)
    .then(function(res2) {
      console.log('res two', res2);
      publicIP = res2.ip;
      console.log('i', publicIP);
      ipArray.push({'ip': publicIP});
      return ipArray;
    })
    .then(MyService.funThree)
    .then(function() {
      console.log('res three');
    });

  promiseArray.push(promise);
}
console.log('pA', promiseArray);
return $q.all(promiseArray);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在for循环中链接猫鼬的诺言

来自分类Dev

在webdriver.io中的while循环中链接诺言

来自分类Dev

for循环中的有角诺言

来自分类Dev

将诺言添加到for循环中的诺言数组中

来自分类Dev

将诺言添加到for循环中的诺言数组中

来自分类Dev

即使语句已正确关闭,循环也不会执行错误

来自分类Dev

Pygame:Rec对象在For循环中无法正确渲染

来自分类Dev

在Try / Except循环中BeautifulSoup无法正确解析HTML

来自分类Dev

在批处理循环中无法获得正确的行为

来自分类Dev

for循环中的变量在shell脚本中无法提供正确的输出

来自分类Dev

在游戏循环中无法正确读取数组值

来自分类Dev

foreach 循环中的数组值无法正确显示

来自分类Dev

等待循环中调用的所有诺言完成

来自分类Dev

为什么不在循环中时,诺言不起作用?

来自分类Dev

在循环中链接嵌套的Promise

来自分类Dev

从foreach循环中链接许诺

来自分类Dev

在 for 循环中创建符号链接

来自分类Dev

如何在BeautifulSoup的for循环中使用.find正确过滤链接?

来自分类Dev

在for循环中顺序执行promise

来自分类Dev

R:在for循环中执行if语句

来自分类Dev

创建函数以正确地链接诺言

来自分类Dev

如何执行正确的循环

来自分类Dev

正确使用foreach循环中的foreach循环

来自分类Dev

正确使用foreach循环中的foreach循环

来自分类Dev

无法在C ++中的循环中将项目添加到链接列表

来自分类Dev

无法生成正确的链接

来自分类Dev

单列循环中的已禁用按钮

来自分类Dev

循环中的Python Apscheduler cron作业无法执行所有不同的版本

来自分类Dev

如果在简单的foreach循环中无法正确评估if语句

Related 相关文章

热门标签

归档