Giter Site home page Giter Site logo

Async/Await about redux-saga HOT 16 CLOSED

redux-saga avatar redux-saga commented on May 3, 2024 15
Async/Await

from redux-saga.

Comments (16)

Andarist avatar Andarist commented on May 3, 2024 5

BTW is there any docs page discussing A/A vs redux-saga?

No, various comments might be found across issues, but they are not consolidated into 1.

Is the reason for async await-based sagas incapable of performing effects the fact, that what is yielded with redux-saga is a POFO object (representing the effect), whereas with A/A it's a promise, natively?

async/await is all or nothing (can result in a single value only), it returns a promise and resolves intermediary promises with builtin mechanism. Generators do not progress on their own - they need to be called (.next()) to progress, whats more - arbitrary values can be injected (.next(value)), we can call .next right away (for sync effects) in the same call stack or can call it later (async effects) - and some effects might even be a mix of sync/async, like take(channel), if there are pending messages in the channel we resolve immediately, if there are not we wait until something pops up in the channel. Generators are producers which can result in many (even infinite) results. Also because whole control is in hands of redux-saga we can implement things like cancellation, which is not possible with async/await.

Generators might seem like an overkill, they are certainly less familiar to most of JS developers - but they are uniquely powerful. You can implement async/await on top of a simple generator-based runner, but you cant do the opposite - it's impossible to implement yielding generator on top of async/await.

from redux-saga.

b4stien avatar b4stien commented on May 3, 2024 3

Hey @yelouafi, the link you mentioned is not reachable anymore, can you update it? This page is the first link googling redux saga and async/away

from redux-saga.

Andarist avatar Andarist commented on May 3, 2024 3

@ducin i guess you can write simple sagas as async functions, they are just functions returning promises with some sugar syntax on top of it, BUT that won't let you yield any effects in those "async" sagas, which is the true power of redux-saga

All places, where we actually yield async stuff would make sense, but call is synchronous - would that be a problem

call is by no means synchronous, it can call synchronous functions, but it can also call asynchronous ones. What matters is that this is a "blocking" effect - called function (sync/async/generator) must finish and only then "calling" saga will get resumed.

And the only reason for call is clean testing (which is , as a side effect, disabling async await)?

True reason for having effects is that they are lazy - which opens new techniques. It's the runtime that executes those effects, so in theory we can modify yielded effect, inspect it etc to have it executed in certain way. As a bonus of being lazy they do not get executed automatically in tests.

from redux-saga.

fhelwanger avatar fhelwanger commented on May 3, 2024 1

I think the link @yelouafi pointed to is in the docs now. Check: http://redux-saga.github.io/redux-saga/docs/basics/DeclarativeEffects.html

from redux-saga.

corysimmons avatar corysimmons commented on May 3, 2024 1

Generators might seem like an overkill, they are certainly less familiar to most of JS developers - but they are uniquely powerful.

Super interesting. Kinda makes me sad async/await exploded in popularity before people (me) really got to explore generators.

Thanks for sticking to guns on generators. Now I have a good reason to grok them.

from redux-saga.

Andarist avatar Andarist commented on May 3, 2024 1

The point is - with generators u don't need to handle signals at all. We can just "abort" at any time. If you are looking for an async/await-based solution then you are not looking for redux-saga. Generators are just used here for a reason and they solve problems that this library was set to solve - they were not chosen on a whim.

from redux-saga.

Andarist avatar Andarist commented on May 3, 2024 1

In this case, the generators are managed by the redux-saga. It "executes" them - so the user doesn't have to worry about this at all. When the saga gets canceled then our runtime simply calls sagaIterator.return() and that's it. The user, usually, doesn't have to do anything to handle the cancellation in their saga.

from redux-saga.

slorber avatar slorber commented on May 3, 2024

@christianalfoni i'm not sure but maybe my answer here could also help answer your question:
#6

from redux-saga.

yelouafi avatar yelouafi commented on May 3, 2024

As @slorber said, async/await makes testing difficult. see Declarative effects section

from redux-saga.

yelouafi avatar yelouafi commented on May 3, 2024

closing this. feel free to comment, I can reopen it if you like

from redux-saga.

ducin avatar ducin commented on May 3, 2024

redux-saga won't be internally rewritten to use async/await instead of lower-level coroutines, according to #987. But is it safe for developers to write sagas as async functions? All places, where we actually yield async stuff would make sense, but call is synchronous - would that be a problem? And the only reason for call is clean testing (which is , as a side effect, disabling async await)?

from redux-saga.

ducin avatar ducin commented on May 3, 2024

@Andarist thanks for great explanation!
Ok, so basically, replacing generators/promises with async/await would limit the range of possibilities. Is the reason for async await-based sagas incapable of performing effects the fact, that what is yielded with redux-saga is a POFO object (representing the effect), whereas with A/A it's a promise, natively?

BTW is there any docs page discussing A/A vs redux-saga?

from redux-saga.

wenfangdu avatar wenfangdu commented on May 3, 2024

Also because whole control is in hands of redux-saga we can implement things like cancellation, which is not possible with async/await.

@Andarist async/await cancellation is certainly possible with AbortSignal.

from redux-saga.

Andarist avatar Andarist commented on May 3, 2024

Yes, but that forces the author of the async function to handle it at each step of the async task, propagate it to all of its child "tasks" (aka other async functions) etc.

async thisIsHowCancelableAsyncFunctionLooksLike(signal) {
  const res = await doSmth(signal)
  if (signal.aborted) throw new DOMException('AbortError')
  const res2 = await doAnotherThing(res, signal)
  if (signal.aborted) throw new DOMException('AbortError')
  return res2
}

Do you want to write code like that? Cause I don't.

from redux-saga.

wenfangdu avatar wenfangdu commented on May 3, 2024

@Andarist Thanks for the quick response, but the cancellation rarely happens, in reality, the code will look like this:

async function thisIsHowCancelableAsyncFunctionLooksLike(signal) {
  const res = await doSmth()
  return thisNeedsAbort(res, signal) // signal is handled within thisNeedsAbort
}

from redux-saga.

wenfangdu avatar wenfangdu commented on May 3, 2024

@Andarist Thanks for the insight, could you please provide a minimal example of manual aborting with a generator? Never used generators in depth before since they are somewhat confusing to me.

from redux-saga.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.