I'm trying to understand $.when(...).then(...).done(...)
. This is just tests.
Html:
<p id="phrase"></p>
<button id="btn">get phrase</button>
javascript:
var p = $('#phrase');
function displayResult(o){
console.log(o);
p.append(o + ' ');
};
function getSubject(){
console.log('getSubject');
return $.get(
'sujet.js', //returns "the quick brown fox"
displayResult
);
}
function getVerb(){
console.log('getVerb');
return $.get(
'verbe.js',//returns "jumps over"
displayResult
);
}
function getComplement(){
console.log('getComplement');
return $.get(
'complement.js', //returns "the lazy dog"
displayResult
);
}
function close(){
p.append("!!!");
}
function load(){
$.when(getSubject())
.then(getVerb)
.then(getComplement)
.done(close);
}
$('#btn').click(load);
As I understand it, it should:
- request for
sujet.js
, display the string in the paragraph. - request for
verbe.js
, display the string in the paragraph. - request for
complement.js
, display the string in the paragraph. - append "!!!" to the paragraph.
All it does is getting sujet.js
. displayResult
is never called, neither the functions passed to then
and done
.
So, I probably misunderstood something.
I got the example from this answer.
EDIT
My problem was a parse error, not related to $.when
or $.then
. Once fixed, the rest worked as expected. However I marked meagar's answer as accepted because it explained well how the thing works.