Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid infinite dispatching if promise rejects with itself. Fixes #33. #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/__tests__/promiseMiddleware-test.js
Expand Up @@ -85,4 +85,44 @@ describe('promiseMiddleware', () => {
'here you go'
]);
});

it('handles promises that reject with itself', async () => {
// Thenable that synchronously rejects with itself (like jqXHR in jQuery 2)
const selfRejectingSync = {
then(_, eb) {
return Promise.resolve(eb(selfRejectingSync));
}
};

await expect(dispatch({
type: 'ACTION_TYPE',
payload: selfRejectingSync
})).to.eventually.be.rejectedWith(selfRejectingSync);

expect(baseDispatch.calledOnce).to.be.true;
expect(baseDispatch.firstCall.args[0]).to.deep.equal({
type: 'ACTION_TYPE',
payload: selfRejectingSync,
error: true
});

// Promise that rejects with itself (like jqXHR in jQuery 3)
const selfRejectingAsync = new Promise((_, reject) => {
setTimeout(() => {
reject(selfRejectingAsync);
}, 1);
});

await expect(dispatch({
type: 'ACTION_TYPE',
payload: selfRejectingAsync
})).to.eventually.be.rejectedWith(selfRejectingAsync);

expect(baseDispatch.calledTwice).to.be.true;
expect(baseDispatch.secondCall.args[0]).to.deep.equal({
type: 'ACTION_TYPE',
payload: selfRejectingAsync,
error: true
});
});
});
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -12,7 +12,7 @@ export default function promiseMiddleware({ dispatch }) {
: next(action);
}

return isPromise(action.payload)
return isPromise(action.payload) && !action.error
? action.payload.then(
result => dispatch({ ...action, payload: result }),
error => {
Expand Down