Why doesn't the code after await run right away? Isn't it supposed to be non-blocking?
Asked Answered
G

1

46

I have a hard time understanding how async and await works behind the scenes. I know we have promises which make our non blocking code by using the "then" function we can place all the work we need to do after the promise is resolved. and the work we want to do parallel to promise we just write it outside our then function. Hence the code becomes non blocking. However i don't understand how the async await makes non-blocking code.

async function myAsyncFunction() {
  try {
    let data = await myAPICall('https://jsonplaceholder.typicode.com/posts/1');
    // It will not run this line until it resolves await.
    let result = 2 + 2;
    return data;
  }catch (ex){
    return ex;
  }
}

See the above code. I cannot move forward until the API call is resolved. If it makes my code blocking code, how is it any better then promises? Or is there something I missed about async and await? Where do i put my code that is not dependent to the await call? so it can keep on working without await holding the execution?

I am adding a Promise code that i would like to replicate in an async await example.

function myPromiseAPI() {
  myAPICall('https://jsonplaceholder.typicode.com/posts/1')
    .then(function (data) {
        // data
    });
   // runs parallel
  let result = 2 + 2;
}
Germinate answered 9/4, 2017 at 2:58 Comment(5)
AFAIK, await requires that the thing it's awaiting be a promise. So it's not really a either/or.Interpretative
It's sort of as if the lines after your "until it resolves" comment are inside a .then(). And your whole function is itself async, so whatever calls it doesn't have to wait for it.Ellery
@Interpretative it also converts the expression in promise if it is not a promise. that what i read in a blogGerminate
@Ellery Yes. its like i put that in .then(). but how do i put it outside a then() in async await scenarioGerminate
@RohailNajam Right, my point was about async calls, though. Using a function that expects a callback in place of that promise would not work.Interpretative
A
90

Just as its name implies, the await keyword will cause the function to "wait" until the corresponding promise resolves before executing the next line. The whole point of await is to provide a way to wait for an asynchronous operation to complete before continuing.

The difference between this and blocking code is that the world outside the function can continue executing while the function is waiting for the asynchronous operations to finish.

async and await are just syntactic sugar on top of promises. They allow you to write code that looks a lot like ordinary synchronous code even though it uses promises under the covers. If we translated your example there to something that explicitly worked with the promises, it would look something like:

function myAsyncFunction() {
  return myAPICall('https://jsonplaceholder.typicode.com/posts/1')
    .then(function (data) {
       let result = 2 + 2;
       return data;
    })
    .catch(function (ex) {
        return ex;
    });
}

As we can see here, the let result = 2 + 2; line is inside a .then() handler, which means it's not going to execute until myAPICall() has resolved. It's the same when you use await. await just abstracts away the .then() for you.

One thing to bear in mind (and I think the point you're looking for) is that you don't have to use await right away. If you wrote your function like this, then you could execute your let result = 2 + 2; line right away:

const timeout = 
    seconds => new Promise(res => setTimeout(res, seconds * 1000));

function myAPICall() {
  // simulate 1 second wait time
  return timeout(1).then(() => 'success');
}

async function myAsyncFunction() {
  try {
    console.log('starting');

    // just starting the API call and storing the promise for now. not waiting yet
    let dataP = myAPICall('https://jsonplaceholder.typicode.com/posts/1');

    let result = 2 + 2;

    // Executes right away
    console.log('result', result);

    // wait now
    let data = await dataP;

    // Executes after one second
    console.log('data', data);

    return data;
  } catch (ex) {
    return ex;
  }
}

myAsyncFunction();

After some clarification, I can see that what you really wanted to know about is how to avoid having to wait for two async operations one by one and instead have them execute in parallel. Indeed, if you use one await after the other, the second won't start executing until the first has finished:

const timeout = 
    seconds => new Promise(res => setTimeout(res, seconds * 1000));

function myAPICall() {
  // simulate 1 second wait time
  return timeout(1).then(() => 'success');
}

async function myAsyncFunction() {
  try {
    console.log('starting');

    let data1 = await myAPICall('https://jsonplaceholder.typicode.com/posts/1');
    // logs after one second
    console.log('data1', data1);

    let data2 = await myAPICall('https://jsonplaceholder.typicode.com/posts/2');
    // logs after one more second
    console.log('data2', data2);
  } catch (ex) {
    return ex;
  }
}

myAsyncFunction();

To avoid this, what you can do is start both async operations by executing them without awaiting them, assigning their promises to some variables. Then you can await both promises:

const timeout = 
    seconds => new Promise(res => setTimeout(res, seconds * 1000));

function myAPICall() {
  // simulate 1 second wait time
  return timeout(1).then(() => 'success');
}

async function myAsyncFunction() {
  try {
    console.log('starting');
    // both lines execute right away
    let dataP1 = myAPICall('https://jsonplaceholder.typicode.com/posts/1');
    let dataP2 = myAPICall('https://jsonplaceholder.typicode.com/posts/2');

    let data1 = await dataP1;
    let data2 = await dataP2;

    // logs after one second
    console.log('data1', data1);
    console.log('data2', data2);
  } catch (ex) {
    return ex;
  }
}

myAsyncFunction();

One alternative way to do this is to use Promise.all() with some array decomposition:

const timeout = 
    seconds => new Promise(res => setTimeout(res, seconds * 1000));

function myAPICall() {
  // simulate 1 second wait time
  return timeout(1).then(() => 'success');
}

async function myAsyncFunction() {
  try {
    console.log('starting');

    // both myAPICall invocations execute right away
    const [data1, data2] = await Promise.all([
        myAPICall('https://jsonplaceholder.typicode.com/posts/1'), 
        myAPICall('https://jsonplaceholder.typicode.com/posts/2'),
    ]);

    // logs after one second
    console.log('data1', data1);
    console.log('data2', data2);
  } catch (ex) {
    return ex;
  }
}

myAsyncFunction();
Acquah answered 9/4, 2017 at 3:15 Comment(12)
I understand this. what my point is that if we want to run anything in parallel to this api call we will just put it next to the then function like i did in my edited question. How will i achieve this "Parallelism" in async awaitGerminate
@RohailNajam Please see my update. You can "start" as many promises in parallel as you want. Once you use await, the function will start waiting on their result.Acquah
oh i did not knew that. Thanks.Germinate
let results = await Promise.all([dataP1, dataP2]); will return an array that would contain results from both data.?Germinate
@RohailNajam Yes, that's correct. I've modified my answer to show how you can use array decomposition to split it into its respective values.Acquah
Thanks for the helpGerminate
"the world outside the function will continue to go on executing while this function is doing its thing" The "world outside" will go on while the function is not doing anything.Benoni
@zeroflagL A nitpick, indeed, but fair enough. I've fixed the wording.Acquah
I think the phrase "The difference between this and blocking code is that the world outside the function can continue executing while the function is waiting for the asynchronous operations to finish." is for me a point of confusion. What exactly keeps running? Does the caller of the async method continue on with the lines of code after the call to the async method that contains an "await" ?Irenics
@JeremyF If the caller of the function uses await or .then, then it likewise would wait until this function completed. But if it called the function without awaiting ("fire and forget"), then yes, it would continue executing. But perhaps more importantly, the "world outside" includes event handlers, setTimeout, etc. that can run even if something is being awaited. They cannot run if there is blocking, non-async code holding up the works.Acquah
Great answer. Thank you for taking the time to write itOmentum
So write a code after "await" is same as write a code inside .then right? And if we need to write a code that can paralell executed, we shouldn't write that code after the await. CMIIWRedraft

© 2022 - 2024 — McMap. All rights reserved.