0

If I have async function and I extract some data in it and want to use them out of async function what should I do ?! Imagine:

Async function Hello(A,B,C){
....
}

in them middle of my program(in then another function).I use async function and call it to extract something then I want to save what I extract to use them in another function.

function blabla(){
  Hello(A,B,C).then_ =>{
  ...
  ...
  Array.push(what i extract in async function)//For store what I extract.
  }
}

I write this code in Async function : Array.push(what i extract in async function) but in out of this async function didn't work ! why like this ? in Async function how we can use what we extract?

  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Krzysztof Krzeszewski Mar 11 '20 at 12:16
  • 1
    As far as I can tell from your code, pushing to the array will work fine but you're trying to *access* the array *before* the async function has finished. Short example that should clarify the issue: https://jsfiddle.net/khrismuc/h0mf2zrt/ So the answer to your question is to wrap all your code in an async function, then use `await` inside to wait for other async calls. Or in other words: everything you want to happen that must use the array data has to happen *after* the async code has finished. –  Mar 11 '20 at 12:20
  • Sorry I change my question, My mean is in another function I want to call async function and store what i extract and use it . – faezanehhabib Mar 11 '20 at 12:27
  • 1
    You changed the code but not really the issue. Code that uses the array needs to run *after* the async function, so you need to return the array at the end of the then-callback, then append a 2nd `.then(...)` and move the code there. You can never go back from async to sync: https://jsfiddle.net/khrismuc/h0mf2zrt/6/ –  Mar 11 '20 at 12:41

0 Answers0