3

I am implementing a generator into my logic and it is not clear for me

function *generator {
    yield synchronousFunc()
    yield asyncFunc()
    // should wait till asyncFunc() is completed 
    // before calling next yield
    yield anotherSynchronousFunc() 
    yield ...
}

how will it work if I convert this generator to async generator with async/await for

for await (const item of generator()) {}

when most of the yields are synchronous.

How async generator will handle the synchronous part of the yields under the hood?

Maybe it is a silly question, but I'm trying to understand how generators works

3
  • 3
    "implementing generator into my login" - why exactly are you doing that? What is your goal? What code will iterate your generator, what values will it yield, and what is done with them? Commented May 27, 2020 at 12:20
  • Ok, now it's "logic" not "login", but the questions remain. Commented May 27, 2020 at 13:13
  • Also, what do you refer to as "async-gen"? Is that a runtime library you are using? Commented May 28, 2020 at 15:46

1 Answer 1

2

You should put an async keyword in front of your generator to convert it to an async generator.

Here is an example you can run

async function* asyncGenerator() {
  yield 1
  yield 2
  yield Promise.resolve(3)
  yield 4
  yield 5
}

(async () => {
  for await (const n of asyncGenerator()) {
    console.log(n)
  }
})()

This resource helped me develop my intuition on generators and async generators.

With regards to how async generators work under the hood, it has less to do with generators and more to do with async and await. Here is async/await under the hood

Unless you are running a lot of cpu intensive workloads in your redux-saga generators, you should be fine to not worry about the extra promise wrapping that occurs with async generators, even if most of your code is sync.

Sign up to request clarification or add additional context in comments.

9 Comments

How async-gen will handle the synchronous part of the yields under the hood? How your answer correlate with my question?
async-gen will wrap synchronous parts in a promise. so it's not the most efficient
Can you elaborate?
async-await under the hood. it's less to do with generators, more to do with async/await itself. async generated iterators will return Promises. sync iterators return sync values. that means when you convert your generator to an async generator, even sync values will be wrapped with a promise
Thank you for your help. My interest in this particular area arise from redux-saga, it is generator based and has an inhouse delay util, but it looks like that I don't have to mark saga's generator as async for it to work, so I started to investigate... If you have more information regarding this topic I would love to hear it, anyway if you replace your original answer with the relevant info to my question from your comments I'll mark it as resolved
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.