A
A
Alexey Ivanov2015-08-06 16:33:01
Node.js
Alexey Ivanov, 2015-08-06 16:33:01

How to stop iterating over an array in neo-async?

There is an array [10, 50, 100, 358]
When iterating, when we meet the value 100, we need to stop the enumeration. How to do it in neo-async (Module)?

var tempArr = [10, 50, 100, 358];

async.eachSeries(tempArr, function(val, index, done){
  console.log('[Перебор массива tempArr] Итерация #' + index);

  if(val == 100) {
    console.log('Нашли нужное значение в массиве! Index = ' + index);
    done(); // Вот здесь нужно вызвать break, но как это сделать в neo-async?
  }
  else done();
});

You can make a fake mistake and the enumeration will end, but this does not suit me, because if this is done, all parent loops will also stop running.
An example of the latter:
var tempArr = [3, 5];
var tempArr2 = [10, 50, 100, 358, 400];

async.eachSeries(tempArr, function(val, index, done){
  console.log('\n -> [Перебор массива tempArr] Итерация #' + index);

  async.eachSeries(tempArr2, function(val2, index2, done2){
    console.log('[Перебор массива tempArr2] Итерация #' + index2);

    if(val2 == 100) {
      console.log('Нашли нужное значение в массиве! Index = ' + index2);
      done2({'break': true}); // Fake Error
    }
    else done2();
  }, done);
},

function(){
  console.log('\n Перебор массив `tempArr` завершен.');
});

At the output we get the following:

-> [Iteration of the array tempArr] Iteration #0
[Iteration of the array tempArr2] Iteration #0
[Iteration of the array tempArr2] Iteration #1
[Iteration of the array tempArr2] Iteration #2
Found the desired value in the array! Index = 2
Iterating over the `tempArr` array is complete.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Kononenko, 2015-08-06
@premas

I don't know what neo-async is, but in all javascript it's always been like this:

var tempArr = [10, 50, 100, 358];

async.eachSeries(tempArr, function(val, index, done){
  console.log('[Перебор массива tempArr] Итерация #' + index);

  if(val == 100) {
    console.log('Нашли нужное значение в массиве! Index = ' + index);
    return false;
  }
  else done();
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question