Top functions reviewed by kandi - BETA
ANSWERAnswered 2020-Nov-27 at 18:54
From your test, it's not entirely clear whether you know what
getReq is supposed to do. You mock it out* to return
123, but assuming the implementation is mostly correct it would actually return a promise of an array of strings where each string is the first character of a
sha (actually it returns undefined, because you don't return the promise chain, and maybe you actually wanted it to return the complete
sha of the first commit rather than the first character of the
sha for every commit?)
Similarly in your previous attempt your mock for
fetch didn't make sense:
To my understanding of the event loop, the following code will output 'Sync 2' then 'Sync 4' first (synchronous code), then 'Promise 3' (fetch returns a Promise which would be placed in the microtask queue and will be executed after the synchronous code finishes and data comes back from the API), finally 'Async 1' (setTimeout would be placed in the macrotask/callback queue with the lowest priority).
However, in the lastest version Chrome, I always got 'Async 1' before 'Promise 3' if I set setTimeout() to 0 like below. Is there anything I misunderstand? Is it possible that a not-yet-resolved Promise gets lower priority than something in the macrotask queue here?...
ANSWERAnswered 2020-May-19 at 12:14
(fetch returns a Promise which would be placed in the microtask queue and will be executed after the synchronous code finishes and data comes back from the API)
This is only true if the Promise that is returned resolves immediately - for example, with
Promise.resolve. If the Promise returned by
fetch resolved immediately, you'd be right, and the
Promise 3 would log before the
fetch does not resolve immediately - it requires the endpoint to respond first, and that takes some time, so you see the
setTimeout log first.
No vulnerabilities reported
Reuse Trending Solutions
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page