Reputation: 234
What i'm doing wrong?
function test()
{
var d = $.Deferred();
var name=prompt("Please enter your name","Harry Potter");
if (name=="aa")
d.resolve();
else
setTimeout(test, 1000);
return d.promise();
}
test().done(function() { alert("It's Ok!"); });
Upvotes: 1
Views: 7402
Reputation: 9719
The reason it doesn't work on the second name input is, you are calling a function test()
, returning the deferred object which then add's the ability to you original call to test()
allow access to callback methods like .then()
, .fail()
and .done()
.
However, if you do not get the input you wan't you are calling the function test()
again, which creates a NEW deferred object. Thus, the original call to test()
which you have added the done()
callback to will never receive the callback.
Change it to:
var d = $.Deferred();
function test()
{
var name=prompt("Please enter your name","Harry Potter");
if (name=="aa")
d.resolve();
else
setTimeout(test, 1000);
return d.promise();
}
test().done(function() { alert("It's Ok!"); });
This was you are referencing the SAME deferred object and not creating a new one each time the function test()
runs.
See it working here
Upvotes: 4