当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - jQuery when.apply stop if one of the deffereds is rejected

问题描述:

I'm sure the answer is pretty simple, but I seem to can't find it. Is there way that in case one of the deffered functions fails (returns d.reject()) in when.apply functions, that whole when stops. All the other functions will not be executed. I tested and even if one of them fails, the others still execute.

<html>

<head>

<title></title>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.js"></script>

<script type="text/javascript">

$(document).ready(function() {

var promises = [];

promises.push(one());

promises.push(two());

$.when.apply($, promises).then(function(success) {

console.log("Promises done");

}, function(error) {

console.log("Task failed: " + error)

});

});

function one() {

console.log("Task One init");

var def = $.Deferred();

setTimeout(function() {

console.log("Task 1: main entry");

def.reject("FAILED");

}, 5000);

return def.promise();

}

function two() {

console.log("Task Two init");

var def = $.Deferred();

setTimeout(function() {

console.log("Task 2: main entry");

def.resolve("ok1");

}, 8000);

return def.promise();

}

</script>

</head>

<body>

</body>

</html>

Output:

Task One init

Task Two init

Task 1: main entry

Task failed: FAILED

Task 2: main entry

分享给朋友:
您可能感兴趣的文章:
随机阅读: