You Probably Haven't Used This JavaScript Loop

  Рет қаралды 3,243

dcode

dcode

Күн бұрын

Пікірлер: 14
@christian-schubert
@christian-schubert Жыл бұрын
You're onto something here. This also works without awaiting the for...of loop as the loop will iterate over the iterable's values programmatically anyway. In other words: for (const user of usersToFetch) { const result = await user; console.log(result); } will also work just fine (although admittedly less elegant 🤪). HOWEVER!, in this regard, it behaves ENTIRELY differently to forEach, map, etc. as these are higher order functions which will in essence accept pending Promises as what they are and move on to the next value! Should you ever be running out of content ideas, this would be a VERY interesting topic to cover IMHO. -- Also, would probably be worth mentioning that top level awaits are now a thing with Javascript modules so there's FINALLY no more need to always wrap your entire codebase in an async function wrapper 😅
@cm3462
@cm3462 Жыл бұрын
@christianschubert5449 Thanks for telling us about top-level await - this is a big deal! Thanks also @dcode for pinning this!
@artemmakhaydinov3586
@artemmakhaydinov3586 9 ай бұрын
Acording to MDN, for await... of looks for [Symbol.asyncIterator] method to cycle through it, and if it doesnt find it, in just use [Symbol.iterator] but wraps returns of next(), return() and throw() inside iterator into resolved or rejected promises. Author uses an array in video and it doesn't contain [Symbol.asyncIterator], so loop just cycling through an array syncronously, fill microtask queue with promises, handling in parallel sequence and await for result, just like regular for... of with await inside it.
@christian-schubert
@christian-schubert 9 ай бұрын
​@@artemmakhaydinov3586 Hey, no, this is not how it works. As I have explained, higher order functions will "run through the loop" if you will, not resolving pending promises. for...of on the other hand will. Maybe to clarify this a bit - even though array.map() / array.forEach() and for .... of ... behave in similar ways (and yield comparable results), they are technically not the same thing. As the name suggests, higher order functions run functions on each iterable, hence e.g. break / continue do not apply (as there is no loop to "jump out" of). This is not the case with for.../ for...in.../ for...of.../ while / do...while...
@artemmakhaydinov3586
@artemmakhaydinov3586 9 ай бұрын
@@christian-schubertfor... of resolve promises due to scope it creates for each iteration?
@brunocastro4193
@brunocastro4193 Жыл бұрын
Awesome video! It's a bit repetitive at times, but I guess that's necessary. Tip: 8:02 -> You don't need to do ctrl-c then ctrl-v. Just press shift+up or down. Saves a little time when we're working long hours.
@s2유아마에브리띵
@s2유아마에브리띵 Жыл бұрын
thank you so much! this is very useful for FE developer.
@anasouardini
@anasouardini Жыл бұрын
I'm waiting for this to expand laterally so we can also have "forAwaitEach" etc, etc.
@rostyslav5334
@rostyslav5334 Жыл бұрын
What if it rejects the promise, it will just throw an error, as I understand?
@offgridvince
@offgridvince Жыл бұрын
Cool!
@FuzeTox
@FuzeTox Жыл бұрын
but I think we can do that with js async generators isn't it?
@FuzeTox
@FuzeTox Жыл бұрын
amazin
@shamelnet48
@shamelnet48 Жыл бұрын
In minute 6, how can all this data be combined into one object
@suelingsusu1339
@suelingsusu1339 Жыл бұрын
🌹🌹🌹🙏🙏🙏👍👍👍👌👌👌🖖🖖🖖
Learn JavaScript Loops in 10 Minutes
10:34
dcode
Рет қаралды 19 М.
Просто o async, await. Без циклов и таймеров. JavaScript
15:55
WebDev с нуля. Канал Алекса Лущенко
Рет қаралды 100 М.
I Turned My Mom into Anxiety Mode! 😆💥 #prank #familyfun #funny
00:32
I tricked MrBeast into giving me his channel
00:58
Jesser
Рет қаралды 28 МЛН
Human vs Jet Engine
00:19
MrBeast
Рет қаралды 188 МЛН
9 JavaScript Features You’ve Never Used
19:36
Conner Ardman
Рет қаралды 29 М.
5 Tips for Writing BETTER For Loops in JavaScript
16:15
James Q Quick
Рет қаралды 70 М.
Async Functions & Await Explained in JavaScript
12:14
dcode
Рет қаралды 13 М.
The Importance of THIS in Javascript
22:59
developedbyed
Рет қаралды 22 М.
The Most Important Skill You Never Learned
34:56
Web Dev Simplified
Рет қаралды 215 М.
Arrow Functions in JavaScript - All You Need To Know!
10:28
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 485 М.
I Turned My Mom into Anxiety Mode! 😆💥 #prank #familyfun #funny
00:32