0

The code below works - but after i updated NODEJS from 4.. to 6.9.1 it suddently throws an error (for each call)...meaning it executes all recursive calls perfectly and afterwards tells me like 20 times the error message below...

"callback" argument must be a function.

I catch the error - and every thing works fine. Its a recursive call - filterTrades. I have been using it like this for a long time - and never had issues with it before. What to do? I guess the code use unnecessary resources to catch some thing (which actually isn't needed)?

var id = setImmediate(filterTrades(rows, indexStart, indexEnd, offset));
clearImmediate(id);
PabloDK
  • 2,181
  • 2
  • 19
  • 42
  • 2
    Does `filterTrades` return a function? – Dave Newton Nov 11 '16 at 17:21
  • The 2 linies is placed INSIDE the function filterTrades(). Like i wrote - its recursive. – PabloDK Nov 11 '16 at 17:26
  • That's not relevant; the parameter to `setImmediate` should be a function, whereas you're *calling* a function, hence the question about what `filterTrades` returns. – Dave Newton Nov 11 '16 at 17:27
  • i answered your question? FilterTrades does not return a function - but it is a function and the code works. But after i updated node it ALSO throws an error... – PabloDK Nov 11 '16 at 17:35
  • Yes, because `filterTrades` doesn't return a function, which is what `setImmediate` expects as a parameter. And no, you didn't answer the question, you told me that `filterTrades` is recursive, which says nothing about what it returns. – Dave Newton Nov 11 '16 at 17:36

2 Answers2

1

@DaveNewton is on the money - you probably want

var id = setImmediate( () => filterTrades(rows, indexStart, indexEnd, offset) );
clearImmediate(id);

and the es5 way

var id = setImmediate( function(){
    filterTrades(rows, indexStart, indexEnd, offset);
})
clearImmediate(id);
skav
  • 1,400
  • 10
  • 16
0

Probably you should replace this code with

filterTrades(rows, indexStart, indexEnd, offset);

and no setImmediate/clearImmediate.

  1. On prior versions of Node.js, calling setImmediate with a non-function like you are doing would do nothing. Then, calling clearImmediate would do nothing. So, to reproduce that behavior, simply perform the side effect (i.e. call filterTrades), but delete all the setImmediate/clearImmediate stuff.

  2. Even on current versions of Node.js, the code proposed by e.g. @skav's answer will do nothing. If you set an immediate and then clear it immediately, that means nothing will ever execute. So @skav's version will just remove the call to filterTrades entirely; it is equivalent to deleting the code entirely.

Domenic
  • 110,262
  • 41
  • 219
  • 271
  • i dont agree. the code as always worked perfect. the DOC say: clearImmediate(immediate)# Added in: v0.9.1 immediate An Immediate object as returned by setImmediate(). Cancels an Immediate object created by setImmediate(). So the way i understand it - is like... is only cancels the return object and not the actually itself... otherwise my code would not run i guess? – PabloDK Nov 11 '16 at 17:43
  • Also... i can directly see and adjust the memory use by using setImmediate/clearImmediate another place in my code... the amount of work between each loop... it has tremendous effect on the performance... so that it does nothing - it simply not true. – PabloDK Nov 11 '16 at 17:48
  • and... how can you say that @Skav's answer will not execute any thing? Its the same as my original code and it does call my code and execute?? – PabloDK Nov 11 '16 at 21:50
  • Skav's answer is not the same as your original code. – Domenic Nov 12 '16 at 22:40