nancy icon indicating copy to clipboard operation
nancy copied to clipboard

What exactly did callLater do?

Open Rabbitzzc opened this issue 4 years ago • 1 comments

I read the asynchronous part (callLater), but couldn’t understand the mechanism. Can you explain it a bit more?

Bug

the code by Promise:

console.log(1)
Promise.resolve(2).then(v=>console.log(v))
console.log(3)
# or
console.log(1)
new Promise(resolve=>resolve(2)).then(v=>console.log(v))
console.log(3)

will console 1 3 2

but the code by Nancy

console.log(1)
Nancy.resolve(2).then(v=>console.log(v))
console.log(3)
# or
console.log(1)
new Nancy(resolve=>resolve(2)).then(v=>console.log(v))
console.log(3)

will console 1 2 3

Rabbitzzc avatar Jun 28 '20 03:06 Rabbitzzc

https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/

The implementation is not microtask, hence the behavior you're seeing, see below comment and my response:

https://medium.com/@zouguo/there-is-one-problem-resolve-and-reject-should-execute-asynchronously-after-the-event-loop-turn-77aded454878

FMI on how callLater works: https://medium.com/@keyvan.m.sadeghi/how-async-works-in-a-promise-implementation-d709d8f71425

keyvan-m-sadeghi avatar Jun 29 '20 05:06 keyvan-m-sadeghi