Redux-Promise 不会阻止被拒绝的承诺
Redux-Promise does not prevent rejected promises
Redux-Promise 说:
如果它收到承诺,它将发送承诺的已解决值。 如果 promise 被拒绝,它不会发送任何东西。
但是当我 运行 下面的代码并故意做一些事情来让 promise 在 action create 中被拒绝时,我也在 reducer 上得到了被拒绝的 promise!它不应该被发送到减速器吗?
请不要只告诉我解决方法,还请告诉我为什么当 Redux-Promise 这么说时会发生这种情况。
动作:
const responce = axios.get(API_URL);
console.log(responce);
return (
{
type: FETCH_WEATHER,
payload: responce,
}
);
响应^是:
减速器:
export default (state = initalState, action) => {
switch (action.type) {
case FETCH_WEATHER:
console.log(action.payload);
return (
[action.payload.data, ...state]
);
default: return (state);
}
}
action.payload^ 是:
重复措辞:
If it receives a promise, it will dispatch the resolved value of the promise. It will not dispatch anything if the promise rejects.
If it receives an Flux Standard Action whose payload is a promise, it will either
- dispatch a copy of the action with the resolved value of the promise, and set status to success.
- dispatch a copy of the action with the rejected value of the promise, and set status to error.
所以,有两种不同的方式来使用这个中间件:
// dispatch a promise directly
dispatch(somePromise);
// dispatch a promise as an action payload
dispatch({type : "SOME_ACTION", payload : somePromise})
“如果 promise 拒绝,则不会发送任何东西”短语指的是第一种用法——将 promise 直接传递给 dispatch()
。您正在以第二种方式使用它 - 将承诺作为有效负载传递。
Redux-Promise 说: 如果它收到承诺,它将发送承诺的已解决值。 如果 promise 被拒绝,它不会发送任何东西。
但是当我 运行 下面的代码并故意做一些事情来让 promise 在 action create 中被拒绝时,我也在 reducer 上得到了被拒绝的 promise!它不应该被发送到减速器吗? 请不要只告诉我解决方法,还请告诉我为什么当 Redux-Promise 这么说时会发生这种情况。
动作:
const responce = axios.get(API_URL);
console.log(responce);
return (
{
type: FETCH_WEATHER,
payload: responce,
}
);
响应^是:
减速器:
export default (state = initalState, action) => {
switch (action.type) {
case FETCH_WEATHER:
console.log(action.payload);
return (
[action.payload.data, ...state]
);
default: return (state);
}
}
action.payload^ 是:
重复措辞:
If it receives a promise, it will dispatch the resolved value of the promise. It will not dispatch anything if the promise rejects.
If it receives an Flux Standard Action whose payload is a promise, it will either
- dispatch a copy of the action with the resolved value of the promise, and set status to success.
- dispatch a copy of the action with the rejected value of the promise, and set status to error.
所以,有两种不同的方式来使用这个中间件:
// dispatch a promise directly
dispatch(somePromise);
// dispatch a promise as an action payload
dispatch({type : "SOME_ACTION", payload : somePromise})
“如果 promise 拒绝,则不会发送任何东西”短语指的是第一种用法——将 promise 直接传递给 dispatch()
。您正在以第二种方式使用它 - 将承诺作为有效负载传递。