- vừa được xem lúc

Blog#69: Converting Callback Functions to Async/Await in JavaScript 😊 (Series: Master English with Technical Doc)

0 0 14

Người đăng: NGUYỄN ANH TUẤN

Theo Viblo Asia

Để giúp các bạn có thể nâng cao trình độ tiếng Anh, Blog này mình sẽ viết bằng tiếng Anh.

Mục tiêu sẽ là, sử dụng Technical Document để học Tiếng Anh. Mình sẽ cố gắng sử dụng ngữ pháp và từ vựng đơn giản nhất (level~A1/A2) để giúp các bạn đọc nó dễ dàng hơn.


Hi, I'm Tuan, a Full-stack Web Developer from Tokyo 😊. Follow my blog to not miss out on useful and interesting articles in the future.

Converting Callback Functions to Async/Await in JavaScript. Async/await is a way to write asynchronous code in JavaScript that looks like normal code. This can make your code easier to write and understand, and can improve its readability and maintainability.

If you have a callback function that you want to convert to use async/await, here's how you can do it:

Step 1: Declare the function as async

To use the await keyword inside a function, you need to declare the function as async. To do this, just add the async keyword before the function definition, like this:

async function doSomething() { // function body goes here
}

Step 2: Use the await keyword to wait for a promise

Instead of using a callback function, you can use the await keyword to wait for a promise to be finished. A promise is an object that represents the eventual success or failure of an asynchronous operation.

Here's an example of how to use await with a promise:

async function doSomething() { const result = await new Promise((resolve) => { // asynchronous operation goes here resolve(/* result of asynchronous operation */); }); // use the result of the asynchronous operation here
}

Example: Converting a callback function to async/await Here's an example of how to convert a callback function to use async/await:

// original callback function
function doSomething(num, callback) { setTimeout(function() { callback(num * 2); }, 1000);
} // converted async/await function
async function doSomething(num) { return new Promise((resolve) => { setTimeout(function() { resolve(num * 2); }, 1000); });
} // usage of converted async/await function
async function callDoSomething() { const result = await doSomething(5); console.log(result); // 10
}

The converted async/await function is easier to read and write than the original callback function. You don't have to use the nested function syntax of callbacks, and you can use the await keyword to pause the function until the promise is finished.

Conclusion

Converting callback functions to async/await can make your code easier to write and understand, and can improve its maintainability. By using the async keyword and the await keyword, you can write asynchronous code that looks like normal code, which can make it easier to debug.

As always, I hope you enjoyed this article and learned something new. Thank you and see you in the next articles!

If you liked this article, please give me a like and subscribe to support me. Thank you. 😊

Ref

Bình luận

Bài viết tương tự

- vừa được xem lúc

async await javascript without try catch

javascript async await. Phần này chúng ta không nói nhiều về khái niệm async await là gì? Mà ở đây chúng ta đi sâu hơn về một vấn đề có nhiều trường hợp thực tế xảy ra khi triển khai với lập trình bất đồng bộ và async function javascript.

0 0 17

- vừa được xem lúc

Async/ await trong phiên bản Swift 5.5

WWDC21 đã kết thúc, nghĩa là phiên bản beta Swift 5.5 đầu tiên sắp ra mắt và nó đi kèm với một loạt các cải tiến - async / await, actors, throwing properties và nhiều cải tiến khác.

0 0 41

- vừa được xem lúc

Tìm hiểu về Synchronous và Asynchronous trong Javascript

1. Giới Thiệu.

0 0 81

- vừa được xem lúc

Flutter - Lập trình bất đồng bộ với Future, async, await.

Vào thẳng chủ đề nhé các bạn, hôm nay mình đọc được một bài viết về lập trình bất đồng bộ trong Dart. Ôi cái chủ đề này nghe quen quen nhợ, .

0 0 32

- vừa được xem lúc

[JS] Promise và async/await: Cuộc chiến không hồi kết hay là sự đồng hành đáng ghi nhận

Xin chào các bạn. Ngày trước khi xử lý bất đồng bộ, cách cơ bản và được hầu hết mọi developer sử dụng đó chính là callback function.

0 0 77