In Javascript, I've ran into an example that creates a batching process via
an asynchronous call with Promise.resolve().then(runDownTheQueue)
in pseudocode it looks something like:
function queueEverythingUp (addMeToQueue) {
if (queueIsEmpty) {
Promise.resolve().then(runDownTheQueue);
}
addToQueue(addMeToQueue);
}
This is just a method for setting up batches to be run in a queue.
My question is, how does the use of a Promise differ from this approach to asynchronous scheduling:
function queueEverythingUp (addMeToQueue) {
if (queueIsEmpty) {
setTimeout(() => {runDownTheQueue()}, 0);
}
addToQueue(addMeToQueue);
}
In both cases it seems that the execution of running down the queue is asynchronously deferred until the queue is built up via synchronous calls to queueEverythingUp.
Is the only difference the internal workings of how Promises and setTimeouts interact with the Javascript engine?