저는 약속을 지키고 진행 상황 알림을 작성하려고했습니다.jQuery 지연 약속 진행 알림
코드는 모든 함수를 올바른 순서로 실행하지만 진행 상황 업데이트는 실제로 발생했을 때와 반대로 해결하기 전에 실행됩니다.
누구든지 내가 잘못하고있는 것을 지적 할 수 있습니까? 여기
function start(x) {
console.log("Start: " + x);
var promise = process(x);
console.log("promise returned");
promise.then(function(data) {
console.log("Completed: " + data);
}, function(data) {
console.log("Cancelled: " + data);
}, function(data) {
console.log("In Progress: " + data);
});
}
function process(x) {
var deferred = $.Deferred();
var promise = deferred.promise();
// process asynchronously
setTimeout(function() {
for (var i=0 ; i<x ; i++) {
sleep(1000);
deferred.notify(i);
}
if (x % 2 === 0) {
deferred.reject(x);
} else {
deferred.resolve(x);
}
}, 0);
return promise;
}
function sleep(sleepDuration) {
var now = new Date().getTime();
while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
}
start(3);
바이올린 : while()
로 구현 https://jsfiddle.net/n86mr9tL/
니스 하나. 모두 지금 취소. –