Skip to content Skip to sidebar Skip to footer

How Does Using Async/await Differ From Using Promises?

Recently I started using (Async & Await). Before this, I used Promise to make my process Asynchronous. Like: example.firstAsyncRequest() .then(firstResponse => { return

Solution 1:

Is there only difference of syntax?

Yes. Your examples are functionally equivalent except that your second example is missing

console.log(thirdAsyncRequest);

...after the third await. (That variable really should be result to match the first code block, or thirdResponse to match the other two response variables).

async/await is syntactic sugar around promise creation and consumption. It's really helpful sugar, but that's all it is. async functions return promises. await expressions let you wait for a promise to settle, either getting the fulfillment value if it's fulfilled or throwing an error if it's rejected. (Since rejections are errors, you can use try/catch to handle them.) An uncaught error in an async function (whether a synchronous error or a promise rejection observed by await) causes the promise the async function returned to be rejected with that error. Otherwise, the function's promise is resolved to whatever the function's code returns: if it returns a non-thenable, the promise is fulfilled with that value; if it returns a thenable, the promise is fulfilled or rejected based on the settlement of that thenable.

(Re "resolve" vs. "fulfill" and such, I've written up this post on promise terminology.)


Post a Comment for "How Does Using Async/await Differ From Using Promises?"