JS Callback vs Promises vs Async Await 2

Youngmin Park
3 min readApr 18, 2021

--

Using Promise:

To use the above create Promise we use then() for resolve and catch() for reject.

let’s take this a step further:

In our created promise condition is “true” and we call demoPromise() then our console logs read:

So if the promise gets rejected, it will jump to the catch() method and this time we will see a different message on the console.

What is Chaining?

Sometimes we need to call multiple asynchronous requests, then after the first Promise is resolved (or rejected), a new process will start to which we can attach it directly by a method called chaining.

So we create another promise:

We chain this promise to our earlier “myFirstPromise” operation like so:

Since our condition is true, the output to our console is:

Once the “hello” promise is chained with .then, subsequent .then utilizes data from the previous one.

Async/Await:

Await is basically syntactic sugar for Promises. It makes your asynchronous code look more like synchronous/procedural code, which is easier for humans to understand.

Syntax of Async and Await:

You can see that we use the “async” keyword for the wrapper function printMyAsync. This lets JavaScript know that we are using async/await syntax, and is necessary if you want to use Await. This means you can’t use Await at the global level. It always needs a wrapper function. Or we can say await is only used with an async function.

Thank you

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Youngmin Park
Youngmin Park

No responses yet

Write a response