What is synchronization in javascript
Each line of code waits for the previous one to finish before proceeding to the next
Example:
<script>
console.log("hii");
console.log("hello");
console.log("how are you");
</script>
o|p
:
hii
hello
how are you
what is asynchronization in javascript
To allow multiple tasks to run independently of each other line asynchronization.
Example:
<script>
console.log("hi");
setTimeout(()=>{
console.log("hello");
},2000);
console.log("end");
</script>
o|p
:
hi
end
hello
what is Call be Hell
As more nested callback are added the code becomes harder to read to read maintain and reason about.
Example:
<script>
function task 1(call back )
set timeout(()=>{
console.log("task");
},1000);
set timeout(()=>{
console.log("plan");
},1000);
</script>
Promise
A JavaScript Promise represents the eventual completion (or failure) of an asynchronous operation and its resulting value. It allows you to write asynchronous code that is more readable and manageable than traditional callback-based approaches, avoiding "callback hell.
Eg:
const my promise = new promise (resolve,rejected)=>{
resolve();
}
types
.pending
.resolved - operation completed successfully
.rejected - operation failed
Happy coding...
Top comments (0)