Thursday, 12 October 2017

What is $.Deferred() and .resolve in jquery

itemprop="text">

What exactly .deferred do what is its
work and where and when we should use .deferred with resolve. In below function I have
two .done callbacks. I want to set two different callback msg. href="http://jsfiddle.net/WLpBB/"
rel="noreferrer">fiddle



var
deferred = $.Deferred();
deferred.done(function(value) {

alert(value);
}).done(function(id){
alert(id)

});

console.log(deferred)

deferred.resolve("hello
world");

class="post-text" itemprop="text">
class="normal">Answer




jQuery.Deferred is
the main tool of jQuery's implementation of the promise
pattern.



Here are a few links, worth reading
:






As for your
specific need : you should use .then() (read the docs for href="http://api.jquery.com/deferred.done/" rel="nofollow
noreferrer">.done() and href="http://api.jquery.com/deferred.then/" rel="nofollow
noreferrer">.then()).



var
deferred = $.Deferred();

deferred.then(function(value) {

alert(value);
return
42;

}).then(function(id){
alert('The answer : ' +
id);
});

console.log(deferred)

deferred.resolve("hello
world");


href="http://jsfiddle.net/WLpBB/2/" rel="nofollow
noreferrer">fiddle




No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...