비동기 기능 목록이 있습니다. 첫 번째 기능이 해결 (또는 거부) 될 때, 나는 다른 사람들을 막을 수있을 것으로 기대했다. 예를 들어완료 후 다른 약속을 차단하는 방법
:
function wait(ms){
return new Promise(function(resolve){
setTimeout(function(){
console.log('waited ', ms);
resolve();
}, ms);
})
}
promise1 = wait(2000);
promise2 = wait(5000);
listPromise = [promise1, promise2];
Promise.race(listPromise).then(function(){
console.log("Very first promise has resolved!");
})
실제 결과는 다음
> waited 2000
> Very first promise has resolved!
> waited 5000
예상 결과는 다음
> waited 2000
> Very first promise has resolved!
를 '당신이 * 정리 * 다른 약속의 실행을 취소하고 가능하게 수행 할, 또는 당신은 단순히 돈합니까 콜백 메소드를 호출하기를 원하지만 반환 값에 대해 신경 쓰지 않더라도 실행은 계속됩니다. [이 답변] (https://stackoverflow.com/questions/30233302/promise-is-it-possible-to-force-cancel-a-promise#30235261)에서 두 가지 상황을 처리하는 방법을 잘 설명합니다. – Svenskunganka