Giter Site home page Giter Site logo

Version 1.x about prism HOT 8 CLOSED

tomkis avatar tomkis commented on June 4, 2024 6
Version 1.x

from prism.

Comments (8)

tomkis avatar tomkis commented on June 4, 2024 3

So let me explain why async/await can't be used for side-effects.

First of all, when you talk about side effects, you need to be more specific what you exactly mean by side effects, since you have mentioned async/await I suspect that you meant API call, however there might potentially be side effects which are not asynchronous, for example logging. When you call console.log it's impurity yet it's totally synchronous operation and would not benefit from async functions at all.

However, I am pretty much convinced that when you mentioned side effects, you actually meant long lived transactions. It's really confusing because even redux-saga claims that it is:

An alternative side effect model for Redux apps

Which frankly, is a nonsense, because redux-saga has nothing to do with side effects. Saga is pattern for Long Lived Transaction (LLT), which in Redux context means series of actions (which may or may not be asynchronous). Typically API call is LLT because it takes more than one action (Request, Response) in place and it lives in time (asynchronous).

async/await is just a syntax sugar on top of Promises, you can just wait for Promise resolving, that's nice, it could help us with our Request <-> Response scenario, but what if our series of actions does not contain only two actions (Request, Response) but may be potentially infinitely long series? It's still transaction, right? It's still long lived, right?

async/await helps you when you need to asynchronously wait for single value. Saga on the other hand can asynchronously wait for many values.

In other words the most trivial Saga definition is something which can accept infinite number of actions and dispatch infinite number of new actions, although it's not just map it's more of flatMap because reaction to one action, may potentially dispatch more than just one action.

And of course, there's also really nice abstraction and it's RxJS Subject ;-) (coming soon)

from prism.

tomkis avatar tomkis commented on June 4, 2024

Please, we are still open to ideas / suggestions therefore feel free to use this issue.

from prism.

tomkis avatar tomkis commented on June 4, 2024

Next has been merged to master and 1.3.1 has been released.

from prism.

jmatsushita avatar jmatsushita commented on June 4, 2024

Have you considered async/await for the side-effects?

from prism.

eliperelman avatar eliperelman commented on June 4, 2024

Async/await cannot handle all the aspects of the side effects and would make testing much more difficult, as has been laid out in redux-saga:

redux-saga/redux-saga#7
http://yelouafi.github.io/redux-saga/docs/basics/DeclarativeEffects.html

from prism.

jmatsushita avatar jmatsushita commented on June 4, 2024

Ah ha I see. But couldn't we use async/await instead of Promises (typically in effects.js) while keeping sagas for their declarative awesomeness?

from prism.

eliperelman avatar eliperelman commented on June 4, 2024

Yes, that is entirely possible, but I'm not sure there is much benefit. redux-saga's call function is already Promise-aware:

// effects.js

// this function returns a promise
export const fetchRandomGif = () => fetch('/gif/random');
import { call } from 'redux-saga/effects';
import { fetchRandomGif } from './effects';

const response = call(fetchRandomGif);
import { fetchRandomGif } from './effects';

const response = await fetchRandomGif();

So as I see it, there is not much to be gained.

from prism.

eliperelman avatar eliperelman commented on June 4, 2024

Also async/await (Async Functions) are mostly sugar on top of Promises. You can't use them without a Promise being created somewhere along the line.

from prism.

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.