New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any plan to rewrite redux-saga using async-await? #987

Closed
FateRiddle opened this Issue May 12, 2017 · 5 comments

Comments

Projects
None yet
5 participants
@FateRiddle
Copy link

FateRiddle commented May 12, 2017

Not a big fan of generator, async-await seems more straight-forward and with a less steep learning curve.
By the way, Koa used to use generator, now their new api adopt async-await, and looks 100 times cooler imo.

@Andarist

This comment has been minimized.

Copy link
Member

Andarist commented May 12, 2017

redux-saga cannot be rewritten to async/await - that uses Promises and its way harder to coordinate 'parallel' tasks with those. It would also mean a big change in semantics, how things actually work. In example select effect is synchronous - cant achieve that with Promises.

Saga being an interpreter of effects is granting us the full control of how and when those effects are resolved + effects are just simple object which are easily comparable and can be monitored which is also way harder with Promises.

Where do u see a steep learning curve with redux-saga? Could something be done to ease that? As to the syntax they both (async and generators) are pretty much the same - just using other keywords. What is done under the hood completely different, but this knowledge is not really that much needed for beginners.

@noahtallen

This comment has been minimized.

Copy link

noahtallen commented Aug 7, 2018

I definitely understand why async/await doesn't integrate with redux saga. But I have some thoughts on why it may be a steep learning curve vs async/await.

Async await doesn't really require any configuration, and can be used in any JS code. It's as simple as having a function marked async and awaiting a promise. Super easy! Additionally, many JS devs are reasonably comfortable with Promises because they are used everywhere. I have an understanding of them based on day-to-day use with many libraries, and async/await just makes using promises way easier. Generators really aren't that common, so it's not a tool many are familiar with. I don't understand them that well myself, even though I'm comfortable with redux-saga.

I think the other part of it is that people aren't just learning the API to redux-saga, but they also have to do a lot of configuration and integration with redux to use it. (Which you obviously can't get around, because you want all of those redux features).

For instance, here is a little function using a generator:

export function * getData () {
  try {
    const data = yield call(databaseFunc, /* parameter is passed using uncommon syntax */)
    yield put({type: 'NEW_DATA', data})
  } catch(e) {
    console.error(e)
  }
}

That's easy to understand! The hard part to understand is that this generator has to be forked from another saga, for instance triggered by a redux action. All that configuration and the forking model isn't common knowledge and requires practice and learning to understand. (not saying that's bad)

Here's what that function looks like with async await. You're right, the syntax looks really similar to redux-saga, but it feels a lot more like the JS we use on a day-to-day basis.

async function getData () {
  try {
    const data = await databaseFunc(/* I could pass parameters using normal function syntax here */)
    // get dispatch somewhere, maybe through a component and pass it to this function
  } catch(e) {
    console.error(e)
  }
}
  1. async/await is syntax that people may be aware of from other languages (e.g. C#)
  2. Everything works like "plain" js except "awaiting" functions
  3. I can call that function directly anywhere in my app right away without setting anything up

I think that's why redux-saga has a higher learning curve than plain promises and async/await. It's a new API that's not quite anything developers have worked with before (yielding, using effects, forking, a lot of new terms to understand). Whereas async/await is just two keywords that help you use promises in a cleaner way. Not really much extra to understand or configure.

that said, I really love redux-saga and all the power it gives. I think it's certainly worth the learning-curve in many cases, but I figured I would try to outline the ways in which I think it's hard for new users.

Realizing this issue was closed a year ago, I should probably get back to fixing the bug I was looking for in the first place :P cheers

@alankar-anand

This comment has been minimized.

Copy link

alankar-anand commented Aug 21, 2018

Here's a way to use async await inside a generator function. stackoverflow

@Andarist

This comment has been minimized.

Copy link
Member

Andarist commented Sep 3, 2018

@ntomallen thanks for the nice feedback, we understand the steep learning curve of redux-saga - but as said before - we just can't use anything other than generators to reach our goals, because only them are giving us the needed flexibility

and as u have noticed, you don't even need to understand generators (although I encourage people to get deeper understanding) to get proficient with the redux-saga

@danenania

This comment has been minimized.

Copy link

danenania commented Oct 31, 2018

@Andarist node 10 has support for async generator functions, meaning we can now use both await and yield inside the same function, however it appears that take/takeLatest/et al do not correctly dispatch to async generators.

I was wondering if there's something fundamental preventing this? It would be nice to be able to choose between the two approaches on a case-by-case basis, especially when using TypeScript, since await allows for type safety while yield does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment