Async Generators - Javascript In Depth

preview_player
Показать описание
We take a look at Async Generators in Javascript together. This builds on the concept of synchronous generators that we looked at earlier and allow us to work with promises in generators in a consistent and easy way. We also compare these new async generators to regular generators that return promises.

We see how we can use the new for-await-of loop to easily loop through async generator objects as well.

Chapters:
00:00 Introduction
00:29 Iterable Recap
01:06 For..Of Loop Recap
01:31 Generator Recap
02:10 Generator For Of Loop
03:02 Generators and Promises
04:02 Generator Promise Code Example
17:04 Async Generator Syntax
18:06 Async Generator Code Example
25:00 Infinite Async Generators
33:16 Async Generator Return Keyword
34:20 Async Generator Yield Delegation
35:55 Next Steps

Рекомендации по теме
Комментарии
Автор

You are very clear in explaining bro! You anticipated all the mistakes I was going to make LOL

fabioescobar
Автор

Generators seem ridiculously powerful, with async generators even more so. I can’t wait to progress and see how we’re going to be using them in tandem with APIs (the idea that we can input into .next() to influence yield returns got my mind racing).

Thanks again Nader you’re helping so many people ✌🏼

tomboolery
Автор

Other youtubers finish these topics in 5 minutes. But your way of teaching is totally different that a student cannot forget the concept. Every person should persue through your course who wants to learn JS

shivamgupta
Автор

Wow!
Your tutorial are great 🖤
Love your content 💓
I wish if you have started earlier so that I should not have to jump between tutorial to know what's actually is happening!
Because you cleared all the concepts as possible so that beginner like me felt comfortable with the Concepts relates to topic 🖤
Wow wow wow👀

Kiran-khadka
Автор

yeah there was a lot going on here, but you've made it clear, thanks

jjjj
Автор

That's very nice thank you for the explanation :)
But how can i mock these functions for testing?

DannyGorelik
Автор

Theres a weird thing going on.
Code similar to what you used:
function* foo_gen() {
yield new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Finished!");
}, 3000);
});
}

for await (const promise of foo_gen()) {
console.log(promise);
}

Gives same error for me in node, but in the browser's console this gives no error and returns "Finished!" after the 3 seconds.
Why?
We can also replace foo_gen() with a `new Promise()` but it will error if it isnt in a array?? [new Promise()]
This feels very strange...

dsstudios
Автор

30:50 you say that for await (const promise of asyncGeneratorObject) {
console.log(promise);
} will not work for normal generator but i try to ran at but it ran just fine.



const normalGenerator = function* () {
let i = 0;

while (true) {
yield new Promise((resolve, reject) => {
setTimeout(() => {
resolve(i);
i++;
}, 1000);
});
}
};

const normalGeneratorObject = normalGenerator();

const asyncGeneratorExecuter = async () => {
for await (const promise of normalGeneratorObject) {
console.log(promise);
}
};

asyncGeneratorExecuter();


this is working fine

ShivamSharma-dqpu