suppose I have this React/Javascript code:
const doVerifyThingy = (thingyId: string): boolean => {
dispatch(verifyThingyInBE(thingyId))
.then((response) => {
return response.payload as boolean;
})
.catch();
return false;
};
const handleButtonClick = () => {
const ok = doVerifyThingy("254dghfgt6346");
if (ok)
go_to_next_step();
else
alert("things are not ok!");
}
When I click a button, handleButtonClick() asks BE if things are alright, and if so we can continue.
The problem is that doVerifyThingy() returns immediately, without waiting for the “.then” event.
So how can I make doVerifyThingy() “thenable” ?
verifyThingyInBE() is by the way async with await calls.
0
You should return promise
from doVerifyThingy
so that we can await
for return value for ok
as:
const doVerifyThingy = (thingyId: string): boolean => {
return dispatch(verifyThingyInBE(thingyId))
.then((response) => {
return response.payload as boolean;
})
.catch(err => {
// DO SOMETHING WITH ERROR
return false;
});
};
const handleButtonClick = async () => {
const ok = await doVerifyThingy("254dghfgt6346");
if (ok)
go_to_next_step();
else
alert("things are not ok!");
}