Posts

Showing posts with the label promise

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 .

JavaScript | Callbacks

JavaScript c allbacks can be used to process the result of async operation. Lets take a example below. As shown in below example the callback function is used to process the response received from rest service. See the Pen by Rohan ( @rohanlopes20 ) on CodePen . Things gets complicated when we have to call more than one request on after another like second request is depending upon response of first request. This leads to callback hell. I recommend to read more on  https://javascript.info/callbacks As you chain more and more calls it becomes complicated to maintained code. This can be avoided with JavaScript Promises. See the Pen by Rohan ( @rohanlopes20 ) on CodePen .