非同步承諾連結
如果你有多個非同步任務需要依次發生,則需要將其 promise 物件連結在一起。這是一個簡單的例子:
function First() {
console.log("Calling Function First");
return $.get("/ajax/GetFunction/First");
}
function Second() {
console.log("Calling Function Second");
return $.get("/ajax/GetFunction/Second");
}
function Third() {
console.log("Calling Function Third");
return $.get("/ajax/GetFunction/Third");
}
function log(results){
console.log("Result from previous AJAX call: " + results.data);
}
First().done(log)
.then(Second).done(log)
.then(Third).done(log);