While loop and async function


#1

I’m in troubles with while llop. I’m trying to execute an async function but I’m not able to let it works fine.

This is my function:

my function name is getP();
....
this.try3().then(ID=>
    {
      console.log( ID);
    }).catch(error => {
        console.log("ERR:"+error)
      }
    )

And then I try to call like:

while(1)
{
     this.getP();
}

But it show my the result only at the end of the cicle. Is there any way to solve this problem. I’have tried ricorsive function, for each but it still dosen’t work.
Thanks


#2

Only by realizing that it’s not a problem at all, but rather part and parcel of how asynchronous operations work.

Read this post. You have to decide which of these three types getP() is. Judging from its name, I would guess it’s a #3, in which case it needs to return the Promise it got from try3(), and the then logic needs to be in getP()'s caller, not in getP itself.


#3

Yes my type is the third one, but still I’m not figure out how to found a way to my case.
I return the promise from inside tr3 and usethen in getP to show the rusult. This is my full code:

try3():Promise<string>{
    return new Promise((resolve, reject) => {
      this.vibratePlugin.stop('',
        function (ID) {
          resolve(ID);
          console.log(ID);
        },
        function (fail) {
          reject("err.");
          console.log("err.");
        });
    });
  }


getP() {
    this.try3().then(ID=>{
      console.log( ID);
    }).catch(error => {
        console.log("ERR:"+error)
      }
    )
  }

Inside a button click event I set a while loop, but i get all results only when loop ends. I need to check the resulst from inside my loop in a if statment.


#4

You simply can’t do that. You’re going to have to rearchitect things so that whatever is designed to happen when you “check the results” happens in a then clause hanging off the promise. That is how promises work.


#5

How could I do that. I’m just using a custom plugin that performe some functions and return a string. I need to check this string and use an if to check if the result is correct or not, and then call again the plugin. I’m looking for a while but promise is the only way I found, nothing more about sync check and launch of the plugin. Could yo suggest any other way?


#6

It doesn’t look like this bold part is true, which is at the crux of the problem. You are using a custom plugin that takes a callback. It doesn’t return anything. You are (rightly so, IMHO) converting that into a plugin that returns a Promise. Everything that cares about what that Promise resolves to must hang off a then clause:

try3().then(id => {
  if (id === "what we are looking for") {
    return doSomethingElseWith(id);
  } else {
    return Promise.reject("didn't expect " + id);
  }
});

#7

I found a way to do what I need. I used a recorsive function to get the value from the plugin callback and wait it.

getP()
 {
        this.try3().then(ID => {
          console.log("id: "+ID);
          if(ID=="xxx")
            //do stuff
          this.getP();
        }).catch(error => {
            console.log("ERR:" + error)
        });
  }

Thanks for the advices @rapropos