Both examples return a Promise
, but there's a key difference that the scripting engine is looking for... Is there anything asynchronous being awaited therein? In the first example there isn't, but in the second example the code at least says there is. (Though I might expect any decent IDE to complain about it, since a string literal isn't an asynchronous operation. The language itself can be pretty forgiving about it though.)
Even if you do perform an asynchronous operation in the first example, unless you await it the surrounding code will still run synchronously. You just wouldn't have any way to observe the result (or failure) of that operation. (Well, you could append .then()
to it, but then why use async
? At that point you'd be mixing your asynchronous conventions.)
If you're calling getData()
within an async
function then you can await it:
console.log(1);
await getData();
console.log(2);
Or, alternatively, you can continue from the returned promise:
console.log(1);
getData().then(() => console.log(2));
Or, more verbosely (or for anybody stuck in IE):
console.log(1);
getData().then(function () {
console.log(2);
});