How to determine when all callbacks have completed

I am looking for a clean JS solution. Do not add to libs, please.

I have a function that throws an XMLHttpRequest indicating standard async operations.

If I call this function, say 5 times, one by one, I set up 5 callbacks for my code. Since JS is the only thread, JS needs to figure out how to cut and cubes the available CPU cycles so that all 5 of my callbacks are completed until completion. It does not say what happens when no callback can rely on anything that other callbacks work on.

If I need to wait until all 5 of my callbacks complete their execution, how can I detect this nonexistent "event"?

I'm not looking for a solution that is based on a combination of things. I am wondering if it is possible to create a real event infrastructure that can be fired to create an event that can be listened to.

+4
source share
1 answer

One way to do this is to increase the number of counters each time each of your original callbacks is returned. This is basically a way to process the async library: https://github.com/caolan/async/blob/master/lib/async.js#L101

+2
source

Source: https://habr.com/ru/post/1496182/


All Articles