I want to implement a deferred base object without using jQuery. Here I will only implement completed and fail-safe callbacks with allow and reject functions. and ofCourse binding the promise method with this function.
I am executing the following implementation in pure js (Edited):
function Deferred() { var d = {}; d.resolve = function() { d.done(arguments); } d.reject = function() { d.fail(arguments); } d.promise = function() { var x = {}; x.done = function(args) { return args; } x.fail = function(args) { return args; } return x; } return d; } var v; var setVal = function() { var d = new Deferred(); setTimeout(function() { v = 'a value'; d.resolve(this); }, 5000); return d.promise(); }; setVal().done(function() { console.log('all done :' + v); });
But the above gives an error: Object #<Object> has no method 'fail'
I know that the return object 'd' of the Deferred() function does not have a done () method. And if I return d.promise from Deferred() , it will not have a permit and reject function.
Please indicate what mistake I am making to achieve the simple goal of a pending object.
Here is the fiddle I'm doing: http://jsfiddle.net/SyEmK/14/
source share