en.javascript.info | Modern JavaScript Tutorial | Learning library
kandi X-RAY | en.javascript.info Summary
kandi X-RAY | en.javascript.info Summary
This repository hosts the English content of the Modern JavaScript Tutorial, published in
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of en.javascript.info
en.javascript.info Key Features
en.javascript.info Examples and Code Snippets
Community Discussions
Trending Discussions on en.javascript.info
QUESTION
I have this javascript code that make a get request. It works fine.
...ANSWER
Answered 2020-Nov-27 at 18:54From 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:
QUESTION
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?
...ANSWER
Answered 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 setTimeout
's.
But fetch
does not resolve immediately - it requires the endpoint to respond first, and that takes some time, so you see the setTimeout
log first.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install en.javascript.info
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page