I am working on a React project. calling function A to set States. having one async function A, need to put the fetch API return result into another asyn function B. the function A is working fine. the fetch method in function B is working fine. but the await is not working in asyn function B. the promisedAdditionalData returned directly as undefined, then only the fetch method (OneFetchFunctionB) working in asyn function B. Please help me out how to make this work, how this function B could also be await for the promised data. thanks!
--update:looks like OneFetchFunctionB(promisedData) is having problem returning data. since if I directly call function B without function A, same problem happened.
--update: problem solved! the problem is I put for loop in OneFetchFunctionB(promisedData). but for loop does not iterate asynchronously. need to put the loop outside the await.I refereed this loop fetch in reactjs Thanks again for everyone's help! BTW, whether or not put the await keyword in the below functionB(promisedData); will not change the result.
if (promisedData[1] > 1) {
functionB(promisedData);
}
original question. is actually working fine:
async functionB(promisedData) {
const promisedAdditionalData = await OneFetchFunctionB(promisedData);
// this Fetch Function B is working fine,
// but calling after promisedAdditionalData returned as undefined.
console.log(promisedAdditionalData); //undefined, calling before the above code, why
if (promisedAdditionalData) {
......
}
}
async functionA() {
const promisedData = await OneFetchFunctionA();
if (promisedData) {
if (promisedData[1] > 1) {
this.functionB(promisedData);
}
else {
...//working fine
}
}
}