Kosal Ang
Mon Jan 08 2024
JavaScript, being single-threaded, often deals with operations that take time to execute, such as fetching data from servers or reading files. To manage these operations effectively without blocking the main thread, JavaScript uses asynchronous programming. Promises and async
/await
are two powerful features that facilitate asynchronous operations in JavaScript.
Promises are objects representing the eventual completion or failure of an asynchronous operation, and its resulting value. They have three states: pending, fulfilled, or rejected.
1const myPromise = new Promise((resolve, reject) => { 2 setTimeout(() => { 3 const data = 'Some asynchronous result'; 4 // If successful 5 resolve(data); 6 7 // If an error occurs 8 // reject('Error occurred'); 9 }, 2000); 10}); 11
1myPromise 2 .then((result) => { 3 console.log(result); 4 // Output: Some asynchronous result 5 }) 6 .catch((error) => { 7 console.error(error); 8 // Output: Error occurred 9 }); 10
async
/await
is syntactical sugar built on top of Promises. It allows writing asynchronous code in a synchronous style, making it more readable and easier to maintain.
1async function fetchData() { 2 try { 3 const result = await myPromise; 4 console.log(result); 5 // Output: Some asynchronous result 6 return result; 7 } catch (error) { 8 console.error(error); 9 // Output: Error occurred 10 throw error; 11 } 12} 13 14fetchData(); 15
async
/await
simplifies writing and understanding asynchronous code, resembling synchronous code structure.Understanding Promises and async
/await
is pivotal for modern JavaScript development. They facilitate handling asynchronous operations effectively, improving code readability, and making it easier to manage complex asynchronous tasks without blocking the main thread.
Asynchronous JavaScript with Promises and async
/await
empowers developers to write more efficient and maintainable code, especially when dealing with time-consuming operations.
Creating a beautiful menu bar using HTML, CSS, and JavaScript involves creating the structure with HTML
JavaScript's closures and scope are fundamental concepts that significantly impact how code behaves
If you're looking to build a responsive sidebar with icons and expandable menus, this HTML and CSS template can serve as a great starting point
Arrays and objects in JavaScript is fundamental for managing and manipulating data
ECMAScript 6, also known as ES6 or ECMAScript 2015, introduced several powerful features that significantly enhanced the capabilities of JavaScript
To get the Monday and Sunday of the week, use the setDate() method to set the day of the month of a date to the Monday and Sunday
Generate all permutations of string and filter unique string by using JavaScript
JavaScript is a versatile scripting language primarily used for client-side web development. It enables interactivity and dynamic content on web pages.
Dropdowns are a feature common to many websites. It's very useful, as they make it easy to show additional data only when it is needed.
TailwindCSS is a popular CSS framework that can help you create nice user interfaces quickly with pre-made CSS classes.