How to do a sequential for with nodejs version 8?

Asked

Viewed 84 times

1

The nodejs version is: V8.10.0

I don’t know much about Ode. In this specific case I need the next iteration to be executed only after the previous iteration has been completed. Also, I need that when an exception occurs, the is be stopped and no more iteration happens.

I thought using break would solve my problem to interrupt the for but did not work.

Note: I really need to do this in version 8, in version 11 I did without problems. But in this version I’m having difficulties.

for (let i = 0; i < modelMatchesList.length; i++) {
    if (someVar) {
         DB.query("example(?,?)", [foo, bar])
         .then((result) => {
             doSomething(result);
         })
         .then(() => {
             doSomething();
         })
         .catch((e) => {
             // Caso tenha alguma excpetion
             // Parar e sair do for
         })

    }
}

4 answers

1

The operator await, which was added in the ES6, serves to simplify the chain of promises, which is what you specified.

await

The operator await is used to wait for a Promise. It can be used only within a async function.

Your converted code would look something like the following:

const execute = async (list, someVar, foo, bar) => {
  for (const item in list ) {
    if (!someVar) {
      continue;
    }

    const result = await DB.query("example(?,?)", [foo, bar]);
    await doSomething(result);
    await doSomething();
  }
};

And you would make the call from that function as follows:

const execution = async () => {
  try {
    await execute(['a', 'b'], true, 1, 'x');
  } catch(e) {
    console.error(e.message);
  }
};

0

Hello, try to add one await before your query.

await DB.query("example(?,?)", [foo, bar])

And in case you stop iteration if you need to, try adding a return false

Example:

for ( let n = 0; n <= 10; n++ ) {
  if ( someVar ) {
    // Some code
  }
  else {
    return false
  }
}

I noticed you’re wearing one .then() after another... I don’t know if I understand your question, but if you want the second .then() only run after completion of the first one, you must, within the first one, redo the database query. Example (you can also use the throw to immediately finish the execution of a script:

DB.query("example(?,?)", [foo, bar])
   .then((result) => {
      if (!result) throw false
      DB.query("example(?,?)", [foo, bar])
       .then((result) => {
           if (!result) throw false
           doSomething()
       })
   })

0

You could try using await before DB.query since it is a precedent, but it will only work if it is inside an async function. It’s good to take a look if Node version 8 supports async and await, it almost certainly does! (I would put this answer more as a comment than a definitive answer but Stackoverflow says I don’t have enough reputation to comment on, so it goes in the same answer)

-1

Let cont = 0;

const limit = modelMatchesList.length;

for (Let i = 0; i < modelMatchesList.length; i++) {

if (someVar) {

    DB.query("example(?,?)", [foo, bar]);

}
count++

if (count == limit) {

    doSomething(result);


}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.