0

I am trying to make potentially dozens of ajax calls and want to stack them out 4 or 5 concurrently but not fire them all at once. In C# I would use an ActionBlock, and send in my delegate until I'm ready and then start executing.

so, I am attempting to build the same in JavaScript, and have been able to get everything working using $.when and arrays, but I am not able to pass in a delegate to a function with particular parameters.

I have tried fn.apply and fn.bind, but both of them execute immediately while trying to create the execution chain.

In c#, the signature would simply be :

public Push(Action|Func<T> methodToCall){...}

Then I would invoke it recursively in the .then handler of the $.when

ewassef
  • 286
  • 2
  • 13
  • 2
    you talk a bit about what you've tried - but haven't shown any of it - perhaps it's a simple mistake you've made, perhaps it isn't - how can we tell? in general, it looks like you may be on the right track – Jaromanda X Jun 05 '17 at 01:50
  • Have you looked at using promises? – ACOMIT001 Jun 05 '17 at 01:53
  • @ACOMIT001 - he mentions `$.when` - so, that would be a yes I'd say – Jaromanda X Jun 05 '17 at 01:54
  • Just like C#, Typescript supports function, in addition it supports async/await in the same manner. It's only a slight twist to get it because it's so similar to C#. – JWP Jun 05 '17 at 02:16

1 Answers1

0

At jQuery you can use .queue(queueName) to set an array of N synchronous functions or asynchronous procedures which can be called in sequence, that is when the previous function returns a Promise or other value; .promise(queueName) to wait for all functions in queueName array to be called; .then() chained to .promise(queueName) to perform action when queueName jQuery promise object is fulfilled.

The same pattern can also be composed without using jQuery; by using Array.prototype.shift(), Promise constructor or other Promise object, and recursion or repeated scheduling.

See

guest271314
  • 1
  • 15
  • 104
  • 177
  • Thanks, the shift() pointed me to the right direction. Queueing the function pointer and params then recursively calling it until the array is empty will work. – ewassef Jun 05 '17 at 23:53