JavaScript | Promises
JavaScript promises are object which used for holding value of Async operation. The value can be either success and failure.
You can refer below links to learn more about promises.
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
- https://javascript.info/promise-basics
In earlier post I'd mentioned about callbacks. Adding multiple callbacks in code make code more complicated and not readable. We can solve this issue with callbacks.
The $.get used already returns promise object so we can directly call then() method of promise object to do processing. We returned the call to second rest so that its result will be available in chain format.
This is useful where we have to call one rest one after another. The fail() at last of chain can be added to catch any errors.
See the Pen Promise by Rohan (@rohanlopes20) on CodePen.
Comments
Post a Comment