Interviewer seni diqqat qilganda ๐
(Promise all)
Interviewer: Can you explain what Promise.all does in JavaScript.
Me: Okay, ๐๐ซ๐จ๐ฆ๐ข๐ฌ๐.๐๐ฅ๐ฅ ๐ข๐ฌ ๐ฅ๐ข๐ค๐ ๐ฉ๐ฎ๐ญ๐ญ๐ข๐ง๐ ๐๐ฅ๐ฅ ๐ฒ๐จ๐ฎ๐ซ ๐๐ ๐ ๐ฌ ๐ข๐ง ๐จ๐ง๐ ๐๐๐ฌ๐ค๐๐ญ. It takes an array of promises and waits for all of them to be resolved. If one promise fails, the whole thing fails.
Interviewer: How does it handle errors.
Me: If any promise in the array rejects, Promise.all immediately rejects with that error, and the remaining promises' results are ignored.
Interviewer: Can you give an example of its usage.
Me: ๐๐ฆ๐๐ ๐ข๐ง๐ ๐ฒ๐จ๐ฎ'๐ซ๐ ๐ฅ๐จ๐๐๐ข๐ง๐ ๐ฆ๐ฎ๐ฅ๐ญ๐ข๐ฉ๐ฅ๐ ๐ข๐ฆ๐๐ ๐๐ฌ ๐จ๐ง ๐ ๐ฐ๐๐๐ฉ๐๐ ๐. ๐๐จ๐ฎโ๐ ๐ฎ๐ฌ๐ ๐๐ซ๐จ๐ฆ๐ข๐ฌ๐.๐๐ฅ๐ฅ ๐ญ๐จ ๐ฐ๐๐ข๐ญ ๐ฎ๐ง๐ญ๐ข๐ฅ ๐๐ฅ๐ฅ ๐ข๐ฆ๐๐ ๐๐ฌ ๐๐ซ๐ ๐๐ฎ๐ฅ๐ฅ๐ฒ ๐ฅ๐จ๐๐๐๐ ๐๐๐๐จ๐ซ๐ ๐๐ข๐ฌ๐ฉ๐ฅ๐๐ฒ๐ข๐ง๐ ๐ญ๐ก๐๐ฆ.
Interviewer: What happens if the array passed to Promise.all is empty?
Me: If the array is empty, Promise.all resolves immediately with an empty array, like ๐ ๐๐๐ฌ๐ค๐๐ญ ๐ฐ๐ข๐ญ๐ก ๐ง๐จ ๐๐ ๐ ๐ฌ.
Interviewer: Can Promise.all handle non-promise values.
Me: Yes, it will treat non-promise values as already resolved promises. So, if you pass a mix of promises and regular values, it will resolve with an array containing the resolved values.
Interviewer: Whatโs the difference between Promise.all and Promise.allSettled?
Me: Promise.all stops as soon as one promise fails, while Promise.allSettled waits for all promises to complete, regardless of their outcomes. So, Promise.allSettled is like checking every egg in the basket to see if itโs good or bad.
Interviewer: How does Promise.all improve performance in some scenarios?
Me: By running multiple promises concurrently rather than sequentially, Promise.all can significantly speed up tasks like making multiple API calls at once, compared to waiting for each one to finish before starting the next.
@Diyorbekk_dev
Post #906
102
- โ 1
- ๐ฅ 1