[英]JHipster - execute code after function is finished
I am using JHipster with react front end and I am having a great problem with the following:我正在将 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 }
});
*/
}
with function createRent
located in another file函数
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;
};
I want to execute the commented code after createRent
is finished.我想在
createRent
完成后执行注释的代码。
I have tried returning a Promise in createRent
and adding .then()
: I get a Property 'then' does not exist.我尝试在
createRent
返回一个 Promise 并添加.then()
:我得到一个属性“then”不存在。
I have tried adding a callback: It does not get executed, because createRent
does not have access to history.我曾尝试添加回调:它不会被执行,因为
createRent
无权访问历史记录。
I have tried adding await
in confirmRent
like this我试过像这样在
confirmRent
添加await
async function confirmRent() {
...
await props.createRent(entity);
/* execute the rest */
}
I get a Unexpected 'await' of a non-Promise (non-"Thenable") value
error.我收到
Unexpected 'await' of a non-Promise (non-"Thenable") value
错误的Unexpected 'await' of a non-Promise (non-"Thenable") value
。
From what I can see, I can not change createRent
signature, because a lot of other functions in other modules depend from it.据我
createRent
,我无法更改createRent
签名,因为其他模块中的许多其他功能都依赖于它。 Does anyone have an idea on how to resolve this issue?有没有人知道如何解决这个问题?
Thank you!谢谢!
I don't know what JHipster is, but if I see correctly, createRent
is not an asynchronous function itself, it just returns one that is, so the props.createRent(entity);
我不知道 JHipster 是什么,但如果我没看错的话,
createRent
本身并不是一个异步函数,它只是返回一个异步函数,所以props.createRent(entity);
call does actually get executed before the code that comes after it. call 确实在它之后的代码之前执行。
[If I wrote const add = x => y => x + y
instead of const add = (x, y) => x + y
, then I would have to call it as add(5)(3)
instead of add(5, 3)
.] [如果我写
const add = x => y => x + y
而不是const add = (x, y) => x + y
,那么我将不得不将其称为add(5)(3)
而不是add(5, 3)
.]
To actually utilize it, you would need to store the value it returns, on which you could use .then()
, like for example:要实际使用它,您需要存储它返回的值,您可以在其上使用
.then()
,例如:
const cr = props.createRent(entity);
cr(dispatch).then(res => {
const rentScsV = (res.value.status >= 200 && res.value.status < 300);
props.history.push({
pathname: "/",
state: { rentScs: rentScsV }
});
)
or you can skip the intermediary variable and call the returned function immediately:或者您可以跳过中间变量并立即调用返回的函数:
props.createRent(entity)(dispatch).then(res => {
const rentScsV = (res.value.status >= 200 && res.value.status < 300);
props.history.push({
pathname: "/",
state: { rentScs: rentScsV }
});
)
@lsti115 last comment gave me an idea so I tried this @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 }
});
});
and it worked.它奏效了。
Is this considered bad coding in any way?这在任何方面都被认为是糟糕的编码吗?
Also I had to cast res as any
because the compiler gave me this error Property 'value' does not exist on type '{}'
.此外,我不得不将
res as any
因为编译器给了我这个错误Property 'value' does not exist on type '{}'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.