I
I
Ilya Pavlov2017-08-06 03:44:19
JavaScript
Ilya Pavlov, 2017-08-06 03:44:19

Execution status of a nodejs async function?

There is an asynchronous function:

var fun = async(function(){
  var list = 0;
  while(await(status())){
    list += await(anotherFun());
  }
  return list;
});

You need to track each new circle of the cycle. The function can be called several times at once, so it makes little sense to write the progress into a separate variable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2017-08-06
@PiCoderman

var counters = [] // массив
var fun = async(function(){
  var state = {val: 0, id: Math.random()} // при запуске новый счетчик
  var counterId = counters.push(state) - 1 // и теперь он доступен снаружи
  var list = 0;
  while(await(status())){
    list += await(anotherFun());
    state.val++ // увеличиваем значение счетчика
  }
  // delete counters[counterId]  // если хотите, можно подчистить
  return list;
});

if you need to track which counter is attached to which function, you can set id before calling

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question