A promise in JavaScript is the same as a promise in real life. When we make a promise in real life, it is a guarantee that someone is going to do something in the future. Because promises can only be made for the future.
A promise may have two possible results: it will either be completed when the time comes, or it fail.
This is also the same for promises in JavaScript. When we define a promise in JavaScript, it will be resolved when the time comes, or it will get rejected. It looks like the IF condition. But there are huge differences between them.
A promise is used to handle the asynchronous result of an operation. JavaScript is designed to not wait for an asynchronous block of code to completely execute before other synchronous parts of the code can run. With Promises, we can defer the execution of a code block until an async request is completed. This way, other operations can keep running without interruption.
Example code:-
0 Comments