Fix a bug with Promise.catch() in PromiseMiddleware
The pending action will be modified and a newly created promise handler
will be returned.
This has (suddenly?) caused errors in tests because the catch() handlers defined in tests would be called before the one defined in PromiseMiddleware:
const p = new Promise(...)
const {payload} = store.dispatch({
payload: new Promise(...),
type: '...',
})
try {
await payload
} catch (err) {
// this handler would be invoked before the catch handler in
// PromiseMiddleware
}
since the PromiseMiddleware adds a then-callback, followed by a
catch-callback.
This commit is contained in:
parent
3398831571
commit
c10d5cf115
@ -225,6 +225,7 @@ describe('createActions', () => {
|
|||||||
error = err
|
error = err
|
||||||
}
|
}
|
||||||
expect(error!).toBeTruthy()
|
expect(error!).toBeTruthy()
|
||||||
|
expect(store.getState().mapping.error).toMatch(/status code 500/)
|
||||||
expect(store.getState().handler.error).toMatch(/status code 500/)
|
expect(store.getState().handler.error).toMatch(/status code 500/)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -24,21 +24,26 @@ export class PromiseMiddleware {
|
|||||||
if (!isPromise(payload)) {
|
if (!isPromise(payload)) {
|
||||||
return next(action)
|
return next(action)
|
||||||
}
|
}
|
||||||
const pendingAction = {
|
|
||||||
...action,
|
|
||||||
status: 'pending',
|
|
||||||
}
|
|
||||||
// Propagate this action. Only attach listeners to the promise.
|
|
||||||
next(pendingAction)
|
|
||||||
|
|
||||||
payload
|
const promise = payload
|
||||||
.then(result => {
|
.then(result => {
|
||||||
store.dispatch({
|
store.dispatch({
|
||||||
...action,
|
...action,
|
||||||
payload: result,
|
payload: result,
|
||||||
status: 'resolved',
|
status: 'resolved',
|
||||||
})
|
})
|
||||||
|
return result
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const pendingAction = {
|
||||||
|
...action,
|
||||||
|
payload: promise,
|
||||||
|
status: 'pending',
|
||||||
|
}
|
||||||
|
// Propagate this action. Only attach listeners to the promise.
|
||||||
|
next(pendingAction)
|
||||||
|
|
||||||
|
promise
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
store.dispatch({
|
store.dispatch({
|
||||||
...action,
|
...action,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user