1

due to everyone's help, I got the logic behind async, promise, then, await.

I have one curisosity on top of its basic nature, which is what if I declare async function but doesn't use await in it.

technically every argument within async function is invisibly capsulated by '.then()' but how it works would be exactly same as synchronous function execution. For example,

async function trackUserHandler() {

  var positiondata= await getPosition();
  var timerdata = await setTimer(2000)

  console.log(positiondata, timerdata);

setTimer(1000).then(() => {
    console.log('Timer done!');
  });
  console.log('one');

}

The console below doesn't run till the first two await function is done due to await(s) sitting before this.

 console.log(positiondata, timerdata);

What if I don't put any await(s) in the async like below?

async function trackUserHandler() {

  var positiondata=  getPosition();
  var timerdata =  setTimer(2000)

  console.log(positiondata, timerdata);

setTimer(1000).then(() => {
    console.log('Timer done!');
  });
  console.log('one');

}

I test-run this code and behaves seemigly same as regular function without 'async'. Of course behind the scene, everything in the function is encapsulated into 'then()' though.

Am I understanding right?

Thank you in advance.

BS100
  • 823
  • 6
  • 22
  • 1
    Does this answer your question? [Async function without await in Javascript](https://stackoverflow.com/questions/45594596/async-function-without-await-in-javascript) – sibabrat swain Jun 01 '20 at 05:57

4 Answers4

3

Behind the scene- If I use Aync but doesn't use await in it, would it be identical with normal function?

Yes, you are right. An async function without an await expression will run synchronously and would be identical with normal function, but the only difference is that async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

For example, the following:

async function foo() {
   return 1
}

...is equivalent to:

function foo() {
   return Promise.resolve(1)
}

If there is an await expression inside the function body, however, the async function will always complete asynchronously.

For example:

async function foo() {
   await 1
}

...is equivalent to:

function foo() {
   return Promise.resolve(1).then(() => undefined)
}

Code after each await expression can be thought of as existing in a .then callback.

palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • Thank you! Super clear but what is the last example? I don't get the part "then()=>undefined" – BS100 Jun 01 '20 at 07:28
  • It simply means that as you are just doing `await 1` and not returning anything thus actually it resolves the promise and when it is resolved inside `.then` callback it just returns undefined. – palaѕн Jun 01 '20 at 07:54
2

Yes, an async function runs synchronously till the first await, so it behaves like a regular function. It does return a Promise though:

 function trackUserHandler() {
   // ... code
   return Promise.resolve(undefined);
 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

In your example, the 2 functions won't behave the same. Without the await keyword, your variables won't capture the results returned by these 2 functions, but instead receive 2 Promises.

var positiondata = getPosition();
var timerdata = setTimer(2000);

So your console.log will print out 2 Promises instead of the values you actually expect.

Duc Nguyen
  • 836
  • 6
  • 12
1

An async function can contain an await expression, that pauses the
execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value.

As you assumed, if no await is present the execution is not paused and your code will then be executed in a non-blocking manner.

const getPromise = async (s) => {    
   return new Promise((resolve, reject) => {
      setTimeout(() => resolve(s), 500);
   });
}

(async() => {
  try {
    const result = getPromise("a"); //no await, result has not been unwrapped   
    console.log('async/await -> ', result);
  } catch (err) {
    console.log(err);
  }
})();

Async function without await in Javascript

sibabrat swain
  • 1,277
  • 8
  • 20