【发布时间】:2021-06-16 04:26:06
【问题描述】:
我想分别处理履行承诺和拒绝承诺的情况。我看到在我的代码中,传递给 then 的第一个函数总是被调用,即使 promise 被拒绝。
我有这样的代码,在评论中解释了这个问题:
dispatch(reduxActionCreatorHere(/* ...)*/).then((d) => {
if (d.error) { // TS error: `error` missing from `d`, although the promise is rejected and the `error` field is accessible in pure JS (tested in Chromium DevTools)
// ...
}
// ...
}
像这样:
export const resetPassword = createAsyncThunk(
"auth/resetPassword",
async (options: { email: string }, thunkAPI) => {
debugger;
return await new Promise<void>((resolve, reject) => {
setTimeout(() => {
requestPasswordCrud(options.email).then(
() => {
console.log("password reset");
resolve();
},
() => {
reject();
}
);
}, 1000);
});
}
);
更新 1
实际的错误信息是:
Property 'error' does not exist on type 'PayloadAction<void, string, { arg: { email: string; }; requestId: string; }, never> | PayloadAction<unknown, string, { arg: { email: string; }; requestId: string; aborted: boolean; condition: boolean; }, SerializedError>'.
Property 'error' does not exist on type 'PayloadAction<void, string, { arg: { email: string; }; requestId: string; }, never>'. TS2339
【问题讨论】:
标签: typescript promise compiler-errors dispatch redux-toolkit