Comments (8)
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.
Please, we are still open to ideas / suggestions therefore feel free to use this issue.
from prism.
Next has been merged to master and 1.3.1
has been released.
from prism.
Have you considered async/await for the side-effects?
from prism.
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.
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.
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.
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)
- Don't Call PropTypes HOT 3
- Sibling components interaction best practice HOT 6
- Performance HOT 2
- Request to join cdnjs HOT 1
- Build a umd package for Browser usage
- Reduce bundle size by using rxjs modularity HOT 1
- Mount order and saga registering HOT 8
- Get currentUser state in component Updater HOT 3
- Sagas not getting unmounted HOT 2
- Version 4.x.x HOT 7
- redux-saga update HOT 2
- Documentation links 404 HOT 5
- Where is documentation for v3? HOT 2
- Props 'selector' and 'wrapper' get passed down to enhanced component HOT 3
- prism-redux/escapeRegexp.js is missing a character in the regex
- Questions about using side-effect libraries with prism
- How to wrap/unwrap dynamic components? HOT 2
- Provide a wrapper compatible with the library-provided unwrapper
- EnhanceComponent doesn't work with thunk middleware
- Dynamic list of stateful components HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prism.