Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,217,843 members, 8,035,622 topics. Date: Monday, 23 December 2024 at 03:54 PM |
Nairaland Forum / Science/Technology / Programming / Deferred / Promise Explained (javascript) (2559 Views)
Were Can I Download Free Video Tutorials On Javascript Or Ruby / Javascript With NodeJS / The Java Virtual Machine Explained (2) (3) (4)
(1) (Reply)
Deferred / Promise Explained (javascript) by kadeerna: 1:11am On Jul 22, 2012 |
jQuery 1.5 saw a rewrite of $.ajax introducing Promises (sometimes called Deferreds) providing amongst other benefits, a cleaner way of managing callbacks on success/failure. Prior to the rewrite, the implementation of $.ajax made scenarios like the following a mess. Making multiple AJAX requests in which the parameters of a request depended on the sucess and return value of the request before (nested callbacks). There was not a simple way to pass multiple callbacks to be called on success or failure of the request. Hacks like creating an array of functions to be called, creating a function that called the functions in the array, checking return values, and other operations made application code unclean and unmanageable polluting biz logic and bloating LOC unecessarily. Lets see some code to handle multiple callbacks on success or error of a request. Example:
Developers saw a common pattern in recurring uses of methods like the above and factored out a proposal called the Promises/A proposal. The dojo library V0.9 was one of the first libraries to ship with a Promise/A implementation. The reasons I gave above are only some of the higher level reasons for factoring a common spec. Several lower level reasons exist. Check out the web for more on the Promise/A. In the example listing above, all callbacks for error and success had to be registered before initiating the AJAX request. The promise pattern allowed code to be written to say perform this/these set of operations (asynchronously), and call on me when you where done. The called code said, aight, take this promise from me that I would do just that. The promise pattern takes the concept further to say, if you ask me call more callbacks later, way way later after I have done and completed (or failed) the async operation, I will call the newly registered callbacks too. Tweaks can be made to call all registered callbacks all again when a new callback is added to the list, or just as described call only the newly registered callback. It is that promise that makes the following code possible using jquery > v1.5. (What is the world turning into eh? Computers can drive, think, speak, and now promise? Well, fear not. The kill switch is not far.)
So this doesn't seem too juju'ish, lemme explain in code a bit of what happens inside of $.ajax. When $.ajax is called, it returns an xhr object that implements the Promise API. Actually, let's forget about the XHR API for now, and focus on the promise API. The promise API and object might look like the following.
If the above listing seemed complicated, imagine that was the following
A promise object holds a list of callbacks to be called. When the operation that returns that promise resolves (completes or fails), it asks the promise to fulfill itself by calling the Promise.resolve() function. Promise.resolve goes on ahead and calls all registered callbacks registered through the Promise.done(function). The various Promise pattern implementations improve upon the described method to provide powerful callback mechanisms that allow biz logic to be focused upon. An example is $.when + $.then in jQuery which allows you combine multiple deferred objects and wait for all of the to resolve before calling a callback.
I hope I have been able to explain the concept of Promise / Deferred and you have a better understanding of why you or jQuery makes you use it in your code. |
Re: Deferred / Promise Explained (javascript) by Nobody: 10:51am On Jul 22, 2012 |
to be frank it's a bit complicated here for first time users, although i switch all my AjaX calls to Promises,because its a lot easier to write and far understandable,same concept here also goes for Dojo except we have a when Object that takes a Promise. nice work bro. keep it up this is some quality stuff |
(1) (Reply)
13 Best Techniques To Earn Money As A Web Developer In 2021 / I'm Preparing For Problem Solving Test / So, I Made A Game For Android!
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23 |