2 ms·
re: "Promises have to be resolved in the next turn of the event loop. This is part of the spec." This seems imprecise? There are multiple queues and the detail
by skybrian 9y ago
re: "Promises have to be resolved in the next turn of the event loop. This is part of the spec."
This seems imprecise? There are multiple queues and the details seem to be implementation-specific [1]. The Promises/A+ specification doesn't seem to specify a particular implementation [2].
The reason this might matter is that if you use promises to do something CPU-intensive without doing I/O, it might be possible to starve the event queue, similar to what happens if you're just writing a loop.
On the other hand, the example in the article is waiting on reads, so they'll be delivered through the event queue. Can reading larger chunks at a time and buffering be used to speed that up?
[1] https://github.com/nodejs/node/issues/2736#issuecomment-138607657 https://github.com/nodejs/node/issues/2736#issuecomment-1386...
[2] "This can be implemented with either a 'macro-task' mechanism such as setTimeout or setImmediate, or with a 'micro-task' mechanism such as MutationObserver or process.nextTick. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or 'trampoline' in which the handlers are called."
- kevincox 9y agoI think the main concerns is that the following code happens in the expected order. console.log("First"); promise.then(_ => console.log("Third")); console.log("Second"); This means that even if `promise` is fulfilled already the callback must be delayed at least until the next turn of the event loop.
- skybrian 9y agoYes, it does need to be delayed until the stack is unwound, but it doesn't need to be delayed until after any events already on the main event queue. (I'm not sure what "the next turn of the event loop" means in the presence of multiple queues.)