What is a Promise?
*(in JavaScript)
A promise is an article that may ๐ produce a solitary worth at some point ๐ later on ๐ with either a settled ๐ฏvalue or a cause ๐ฌ that it's not ๐ resolved.*
How Promises Work
๐๐ญ ๐ฐ๐ข๐ฅ๐ฅ ๐ ๐๐ ๐ณ๐ฎ๐น๐น๐ถ๐ป๐ด ๐จ๐ป๐ฑ๐ฒ๐ฟ ๐จ๐ง๐ โ๏ธ ๐จ๐ ๐ญ๐ก๐ ๐ ๐ฉ๐จ๐ฌ๐ฌ๐ข๐๐ฅ๐ ๐ฌ๐ญ๐๐ญ๐๐ฌ:
๐๐๐๐ง๐๐ข๐ง๐ : This is an initial stage of the Promise ๐ค previously ๐ค an operation ๐ starts.
๐๐ ๐ฎ๐ฅ๐๐ข๐ฅ๐ฅ๐๐: This state demonstrates that ๐ the specified operation was finished. โ
๐๐๐๐ฃ๐๐๐ญ๐๐: ๐ซ This state ๐บ๐ธ demonstrates that ๐ the operation ๐ didn't finish. In ๐ฉ this case, a blunder ๐ซ esteem ๐ will be thrown๐.
๐๐ก๐ฒ ๐ค๐๐จ ๐ฒ๐จ๐ฎ ๐ง๐๐๐ ๐ฌ ๐ ๐ฉ๐ซ๐จ๐ฆ๐ข๐ฌ๐?
Promises are utilized ๐ to deal with nonconcurrent activities.
They give ๐ an another way to deal with callbacks by lessening โฌ the callback hell and composing the cleaner code๐ป.
๐๐ก๐๐ญ ๐ ๐๐ซ๐ ๐ญ๐ก๐ ๐ฆ๐๐ข๐ง ๐ซ๐ฎ๐ฅ๐๐ฌ ๐จ๐ ๐ฉ๐ซ๐จ๐ฆ๐ข๐ฌ๐?
๐ค A promise is an item that ๐ supplies a standard-consistent .then() method
๐คA forthcoming promise might change into ๐ค either satisfied or dismissed by the state.
๐คA satisfied or dismissed ๐ซ promise ๐ค is settled and it should not โ๏ธ change into ๐ any ๐บ other state.
๐Once a promise ๐ค is settled, the worth ๐ต should not โ๏ธ change.
Promise Chaining
Since .then() consistently returns another promise, it's feasible to chain promises with exact power over how and where blunders are taken care of. Promises permit you to imitate typical simultaneous code's attempt/get conduct.
What is the contrast among Callbacks and Promises?
A callback function is a function passed into ๐๐ one more function as a contention.
This capacity is conjured inside ๐ the external function to finish an activity. ๐ฌ
The principle Difference between Callback-functions and Promises is that we Attach a Callback-function to a Promise instead of passing it.
So we actually use callback functions with Promises, yet in an unexpected way (chaining).
Hope you found it useful...
Top comments (0)