gpt4 book ai didi

javascript - JHipster - 函数完成后执行代码

转载 作者:行者123 更新时间:2023-12-02 22:19:46 26 4
gpt4 key购买 nike

我正在使用 JHipster 和 React 前端,但我遇到了以下问题:

function confirmRent() {
const { rentEntity } = props;
const entity = {
...rentEntity,
...rentValues
};
props.createRent(entity);
/* execute this after createRent has finished
const rentScsV = (res.value.status >= 200 && res.value.status < 300);
props.history.push({
pathname: "/",
state: { rentScs: rentScsV }
});
*/
}

函数 createRent 位于另一个文件

export const createRent: ICrudPutAction<IRent> = entity => async dispatch => {
const result = await dispatch({
type: ACTION_TYPES.CREATE_RENT,
payload: axios.post(apiUrl, cleanEntity(entity))
});
dispatch(getEntities());
return result;
};

我想在createRent完成后执行注释的代码。

我尝试在 createRent 中返回 Promise 并添加 .then():我得到一个属性“then”不存在。

我尝试添加回调:它没有被执行,因为 createRent 无法访问历史记录。

我尝试像这样在 confirmRent 中添加 await

async function confirmRent() {
...
await props.createRent(entity);
/* execute the rest */
}

我收到一个Unexpected 'await' of a non-Promise (non-"Thenable") value 错误。

据我所知,我无法更改 createRent 签名,因为其他模块中的许多其他功能都依赖于它。有人知道如何解决这个问题吗?

谢谢!

最佳答案

@lsti115 最后的评论给了我一个想法,所以我尝试了这个

new Promise(resolve => {
resolve(props.createRent(entity));
}).then(res => {
const rentScsV = ((res as any).value.status >= 200 && (res as any).value.status < 300);
props.history.push({
pathname: "/",
state: { rentScs: rentScsV }
});
});

并且成功了。

这是否被认为是糟糕的编码?

此外,我还必须将 res 转换为 any 因为编译器给了我这个错误类型“{}”上不存在属性“值”

关于javascript - JHipster - 函数完成后执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59269535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com