最も簡単なアプローチは、,twoProcess
をパラメータとして使用して$.when()
を利用することです。
var oneProcess = new $.Deferred(function(dfd) {
setTimeout(function() {
dfd.resolve("oneProcess complete")
}, Math.floor(Math.random() * 1500));
return dfd.promise()
}).then(log);
var twoProcess = new $.Deferred(function(dfd) {
setTimeout(function() {
dfd.resolve("twoProcess complete")
}, Math.floor(Math.random() * 1500));
return dfd.promise()
}).then(log);
var threeProcess = function(msg) {
log(msg);
return new $.Deferred(function(dfd) {
setTimeout(function() {
dfd.resolve("threeProcess complete")
}, Math.floor(Math.random() * 1500));
return dfd.promise()
})
}
function log(msg) {
console.log(msg);
}
var checkOneTwo = $.when(oneProcess, twoProcess);
checkOneTwo.then(function(one, two) {
threeProcess(
"oneProcess state:" + oneProcess.state()
+ ", twoProcess state:" + twoProcess.state()
).then(log)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
別の方法としては、両方のoneProcess
をチェックするために、各繰延オブジェクトに.state()
を呼び出すためsetInterval
、twoProcess
deferred.state()
は"resolved"
を返すように呼び出して利用することができます。
var oneProcess = new $.Deferred(function(dfd) {
setTimeout(function() {
dfd.resolve("oneProcess complete")
}, Math.floor(Math.random() * 1500));
return dfd.promise()
}).then(log);
var twoProcess = new $.Deferred(function(dfd) {
setTimeout(function() {
dfd.resolve("twoProcess complete")
}, Math.floor(Math.random() * 1500));
return dfd.promise()
}).then(log);
var threeProcess = function(msg) {
log(msg);
return new $.Deferred(function(dfd) {
setTimeout(function() {
dfd.resolve("threeProcess complete")
}, Math.floor(Math.random() * 1500));
return dfd.promise()
})
}
function log(msg) {
console.log(msg);
}
var interval = null;
var checkOneTwo = function() {
var oneTwo = new $.Deferred();
interval = setInterval(function() {
console.log(oneProcess.state() === "resolved"
, twoProcess.state() === "resolved");
if (oneProcess.state() === "resolved" &&
twoProcess.state() === "resolved") {
clearInterval(interval);
oneTwo.resolve()
}
}, 100);
return oneTwo.promise().then(function() {
return threeProcess(
`oneProcess state:${oneProcess.state()}`
+ `twoProcess state:${twoProcess.state()}`
)
})
}
checkOneTwo().then(log);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
あなたはおそらく同様に – adeneo
を行い、それらの2つのAJAX要求のためのコードを表示する必要があります 'oneProcess'はと' twoProcess'は常に 'threeProcess'前に終了しますか? – Terminus
最初の2つのプロセスに対して、ajaxの 'complete'ローカルイベントを使用できます。 –