0

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
           }
       }


   }
fairy
  • 9
  • 2
  • 1
    Thanks for contributing to StackOverflow. For functionB, when you say working fine, what does that function return? It could be the OneFetchFunctionB(promisedData) isn't a valid call. FunctionA has promisedData defined since you said it goes into the conditional after. – Elia Ahadi Nov 22 '19 at 02:00
  • Can we see `OneFetchFunctionB`? – kind user Nov 22 '19 at 02:02
  • Hi OneFetchFunctionB(promisedData) is a valid call. I put console everything is output fine. Just everything run after this function B’s promised data. The problem is await in this function B is not working it directly showing promisedAdditionalData as undefined. – fairy Nov 22 '19 at 02:04
  • I don't see `await functionB(…)` - are you sure you've posted correct code? – Alexei Levenkov Nov 22 '19 at 02:07
  • Please see the code. console.log(promisedAdditionalData); //undefined, this showing before other console output in the fetch function. This means the await not working ? – fairy Nov 22 '19 at 02:26
  • @fairy Are you sure that the `undefined` log is coming from there and not somewhere else? How about using `console.log('promisedAdditionalData', promisedAdditionalData)` to make sure? Your call to `functionB(promisedData)` without an `await` in `functionA` looks suspicious to me. Why aren't you `await`ing it? – JLRishe Nov 22 '19 at 02:33
  • @fairy you are calling `functionB` from `functionA` without await that means there is a chance `promisedData` is undefined and hence `OneFetchFunctionB(promisedData)` will return undefined. Are you able to `console.log(promisedData)` in both `functionA` and `functionB` and see their result? – tareq aziz Nov 22 '19 at 02:38
  • @tareqaziz `functionB` is only called when `promisedData` is truthy and `promisedData[1] > 1` is true, so no, promisedData could never be undefined when `functionB()` is called. – JLRishe Nov 22 '19 at 02:41
  • Hi I think the undefined is the one but I will try as you mentioned. For the await part, do you mean this part: if (promisedData[1] > 1) { await functionB(promisedData); } like this ? Then do I need other related keyword async , await in the original question ? I am new to this async nested thing. If you could tell me the correct way to use the nested, please help. Thanks – fairy Nov 22 '19 at 02:41
  • @fairy put await like this way `await functionB(promisedData)` and see the result – tareq aziz Nov 22 '19 at 02:44
  • 1
    @EliaAhadi is correct . looks like OneFetchFunctionB(promisedData) is having problem returning data. since if I directly call function B without function A, same problem happened. thanks everyone for your time and help....I will update once I successfully figure the entire thing out. – fairy Nov 22 '19 at 03:53

0 Answers0