I am just beginning to learn javascript, and have been provided the following answer to one of the js-assessment asynchronous testing problems.
var dfd = $.Deferred();
setTimeout(function() {
dfd.resolve(value);
}, 10);
return dfd.promise();
I believe the above code is supposed to resolve the function after 10 seconds, but I've had trouble understanding the syntax. I am suspicious that it might be deprecated or an older style.
Does the $.Deferred()
represent a promise because of the $? According to here it's just a random variable name (it can be easily be replaced with something like example_promise
but I suspect might be wrong. Does a promise have to have a .resolved()
function inside of it? And last, do I have to specify return dfd.promise()
vs return dfd
?
It might be much to ask, but basically I'd like a line-by-line breakdown of what's going on and why.