确定嵌套FOR循环和IF语句何时完成一个异步函数调用 [英] Identify when nested FOR loops and IF statement with one asynchronous function call are done

查看:186
本文介绍了确定嵌套FOR循环和IF语句何时完成一个异步函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有嵌套for循环和IF语句的函数以及一个异步函数调用。结构看起来类似于:

I have a function with nested for-loops and IF statements and one call of an asynchronous function. The structure looks similar to this:

search(){
    // for loop 1
    for (let i in object){

      // IF statement 1
      if (condition1){

        // for loop 2
        for (let o in object[i]){

          // IF statement 2
          if (condition2){

            // call of an asynchronous function
            this.asyncFunction().then( data => {

              this.result.push(data)

            });
          }
        }
      }
    }
  }

我想在完成所有循环并完成所有异步函数调用后调用一个函数(假设 goToNextPage()),因此,我的 this.result 对象已完成。

I want to call one function (let's say goToNextPage() ) as soon as all loops are done and all async-function calls are completed and, thus, my this.result object is completed.

但是,我不知道如何管理它。我尝试使用计数器,知道何时处理最后一个对象[i] 以及最后一个对象[i] [o] 之后被处理以调用 goToNextPage()。但是如果最后一个对象在if语句中失败,则这不能正常工作。然后调用 goToNextPage(),而倒数第二个对象的异步函数仍在运行。

However, I don't know how to manage this. I tried to work with counters, to know when the last object[i] is handled and when the last object[i][o] was handled to call goToNextPage() afterwards. But this doesn't work properly, if the last object fails one if statement. Then goToNextPage() is called, while the async function of the second to last object is still running.

我不能只在

this.asyncFunction().then( data => {
    this.result.push(data)
    // call it here, if last element is reached
});

从那时起,如果最后一个元素不符合其中一个IF语句,则不会被调用。

since then it would not be called if the last element doesn't meet one of the IF statements.

我希望我的问题是可以理解的,并且有一个解决方案...

I hope it's understandable what my problem is and that there is a solution for this...

谢谢你有时间!

推荐答案

Koleman在官方的Ionic论坛上做了完美的回答,从而帮助我解决了这个问题:链接

Koleman answered this perfectly on the official Ionic forum and thereby helped me with the solution: Link

他的方法是在结构中插入promises并使用 Promise.all (承诺).then(...)等待所有承诺得到解决。

His approach is to insert promises in the structure and to use Promise.all(promises).then(...) to wait until all promises are resolved.

search(){
   let promises = [];
    // for loop 1
    for (let i in object){
        // IF statement 1
        if (condition1){
            // for loop 2
            for (let o in object[i]){
                // IF statement 2
                if (condition2){
                    let promise = new Promise((resolve, reject) => { 
                        this.asyncFunction().then( data => resolve(data)); 
                    });
                    promises.push(promise); 
                }
            }
        }
    }

    return new Promise((resolve, reject) => {
        if(!promises.length){
            reject('Something went worng!');
        }else{
            Promise.all(promises).then((results) => {
                this.result = this.result.concat(results);
                resolve('We done it!');
            });
        }        
    });         
  }

  search().then(
    successText => {
        console.log(statusText);
        console.log(this.result);
    },
    failText => {
        console.log(failText);
    }
  );

这篇关于确定嵌套FOR循环和IF语句何时完成一个异步函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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