Promise and Observable difference:
* Promises are eager in nature and observables are lazy in nature.
* Observables are lazy so that's why they are not executed until we subscribe to them.
* Promises are eager (not lazy): It executes immediately just after creation
** example below for promises:
data is getting printed just after creation of the promise2
const promise2= new Promise((resove,reject)=>{
let data= "dfghjk";
console.log(data)
})
output : //dfghjk
#javascript#promise#asynchronousjavascript#DEVCommunity
JavaScript bugs aren't always about syntax. Sometimes the code is correct...but the timing is wrong.
Last week I hit a race condition that silently broke my UI.
Here's the fix I wish I knew earlier 👇
#JavaScript
3. Async/Await: A syntactic sugar over Promises, making asynchronous code look synchronous and more readable.
Example:
async function fetchData() {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("Data received"), 2000);
});
let result = await promise; // wait until the promise resolves
console.log(result);
}
fetchData();
Understanding these approaches to asynchronous programming is key to building efficient and responsive JavaScript applications.
Embrace the asynchronous nature of JavaScript for a smoother user experience!
#AsynchronousJavaScript#coding
I just release a new article on Hashnode about Async JavaScript. Check it out give comments and share.
Unlocking the Power of Asynchronous JavaScript: A Comprehensive Guide
{ by @bukoye_olaniyi } from @hashnode#javascript#asynchronousjavascript