erikras / redux-promise-listener Goto Github PK
View Code? Open in Web Editor NEWA Redux middleware that allows actions to be converted into Promises
License: MIT License
A Redux middleware that allows actions to be converted into Promises
License: MIT License
If I fire up multiple requests where each request takes some time before it gets resolved all requests are getting resolved with the first request which succeeds.
async getUser(request) {
return new Promise(resolve => {
const generatedAsyncFunction = promiseListener.generateAsyncFunction({
start: 'START_ACTION_TYPE',
resolve: 'RESOLVE_ACTION_TYPE',
reject: 'REJECT_ACTION_TYPE'
});
generatedAsyncFunction.asyncFunction({ request }).then(
resolvePayload => {
resolve(resolvePayload);
},
rejectPayload => {
resolve(rejectPayload);
}
)
});
};
// All requests are getting resolved with A
getUser('A').then(console.log);
getUser('B').then(console.log);
getUser('C').then(console.log);
https://github.com/erikras/redux-promise-listener#step-1
const reduxPromiseListener = createReduxPromiseListener =>MISSING ()
Hurts the brain when you copy and paste and suddenly get 'e is not a function'.
Thanks, works great with redux-saga
This is a bug report.
When a promise is rejected the error is not being caught.
The error being caught.
https://codesandbox.io/s/qqwn5vn1rj
I'm using redux-promise-listener v1.1.0 and react-redux-promise-listener v1.0.0
I've described the issue here also.
Hi,
redux-promise-listener
doesn't have typescript type definitions at the moment. I tried to convert existing flow type definitions and I ended up with this:
declare module 'redux-promise-listener' {
import { Store } from 'redux';
export type State = any;
export interface Action {
type: string;
payload?: any;
}
export type Next = (action: Action) => State;
export type SetPayload = (action: Action, payload: any) => Action;
export type GetPayload = (action: Action) => any;
// I'm not sure about this generic
export type Middleware<S = any> = (store: Store<S>) => (
(next: Next) => (
(action: Action) => State
)
);
export interface Config {
start: string;
resolve: string;
reject: string;
setPayload?: SetPayload;
getPayload?: GetPayload;
getError?: GetPayload;
}
export interface AsyncFunction {
asyncFunction: (...args: Array<any>) => Promise<any>;
unsubscribe: () => void;
}
export type PromiseListener = {
middleware: Middleware,
createAsyncFunction: (config: Config) => AsyncFunction
};
export default function createListener(): PromiseListener;
}
I'm not proposing a pull request because I'm not sure if this declaration is correct and whether you find this idea useful.
Thank you.
@erikras Hey there! Just interested that if I increase the support of react in package.json would you consider merging it? It would help out with migration from react 16 projects to higher versions.
bug report
I use the provided code of step 1 and step 2 and get the following error:
Error: "Actions may not have an undefined "type" property. Have you misspelled a constant?"
.
const generatedAsyncFunction = promiseListener.createAsyncFunction(
'START_ACTION_TYPE',
'RESOLVE_ACTION_TYPE',
'REJECT_ACTION_TYPE',
);
const values = {
type: REQUEST_ITEMS,
payload: { eventName, request },
};
generatedAsyncFunction.asyncFunction(values).then(
resolvePayload => {
console.log('foo: ', resolvePayload);
},
rejectPayload => {
console.log('bar: ', rejectPayload);
},
);
generatedAsyncFunction.unsubscribe();
To not get an error.
node: 8.12.0
redux-promise-listener: 1.1.0
macOS: 10.13.6
Error: "Actions may not have an undefined "type" property. Have you misspelled a constant?"
performAction http://localhost:8080/:1:40951
liftAction http://localhost:8080/:1:34375
dispatch http://localhost:8080/:1:38408
middleware webpack:///./node_modules/redux-promise-listener/dist/redux-promise-listener.es.js?:46:16
sagaMiddleware webpack:///./node_modules/redux-saga/dist/redux-saga-core.esm.js?:1414:22
dispatch webpack:///./node_modules/redux/es/redux.js?:571:18
asyncFunction webpack:///./node_modules/redux-promise-listener/dist/redux-promise-listener.es.js?:61:9
asyncFunction webpack:///./node_modules/redux-promise-listener/dist/redux-promise-listener.es.js?:60:14
t0 webpack:///./src/rumo-socket.js?./node_modules/babel-loader/lib:608:19
Promise webpack:///./node_modules/core-js/library/modules/es6.promise.js?:177:7
_callee11$ webpack:///./src/rumo-socket.js?./node_modules/babel-loader/lib:594:33
tryCatch webpack:///./node_modules/regenerator-runtime/runtime.js?:62:37
invoke webpack:///./node_modules/regenerator-runtime/runtime.js?:288:22
method webpack:///./node_modules/regenerator-runtime/runtime.js?:114:16
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:5:24
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:27:9
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:34:7
Promise webpack:///./node_modules/core-js/library/modules/es6.promise.js?:177:7
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:23:12
_emit webpack:///./src/rumo-socket.js?./node_modules/babel-loader/lib:665:16
_callee$ webpack:///./src/rumo-socket.js?./node_modules/babel-loader/lib:86:24
tryCatch webpack:///./node_modules/regenerator-runtime/runtime.js?:62:37
invoke webpack:///./node_modules/regenerator-runtime/runtime.js?:288:22
method webpack:///./node_modules/regenerator-runtime/runtime.js?:114:16
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:5:24
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:27:9
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:34:7
Promise webpack:///./node_modules/core-js/library/modules/es6.promise.js?:177:7
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:23:12
request webpack:///./src/rumo-socket.js?./node_modules/babel-loader/lib:102:16
_callee4$ webpack:///./src/rumo-socket.js?./node_modules/babel-loader/lib:187:24
tryCatch webpack:///./node_modules/regenerator-runtime/runtime.js?:62:37
invoke webpack:///./node_modules/regenerator-runtime/runtime.js?:288:22
method webpack:///./node_modules/regenerator-runtime/runtime.js?:114:16
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:5:24
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:27:9
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:34:7
Promise webpack:///./node_modules/core-js/library/modules/es6.promise.js?:177:7
node_modules babel/runtime-corejs2/helpers/asyncToGenerator.js?:23:12
PUT webpack:///./src/rumo-socket.js?./node_modules/babel-loader/lib:206:16
Scratchpad/1:55:9
FEATURE REQUEST
Hi,
This would be great for us to get the full benefit out of redux-form whilst using redux-saga but unfortunately we need more fine tuned control over the actions that will resolve or reject the promise.
The reason is, we have a custom api request handling thing that uses generic API_REQUEST
, REQUEST_PENDING
, REQUEST_SUCCESS
etc. action types with the specific endpoint and params requested stored under a meta
key.
So we need to be able to resolve or reject the promise by looking at the whole action. How about making generateAsyncFunction
take optional function for customising the behaviour?
const generatedAsyncFunction = promiseListener.generateAsyncFunction(
'START_ACTION_TYPE',
action => action.type === 'REQUEST_SUCCESS' && action.meta.endpoint = '/api/v3/users',
action => action.type === 'REQUEST_FAILED' && action.meta.endpoint = '/api/v3/users',
)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.