我最近在看 promise 相关的知识,看到了一个问题:
我测试了一下被采纳的答案,发现运行正常。
于是我又改写了一下,如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript" src='/jquery-1.12.4.js'></script>
<script type="text/javascript">
var task1 = function(){
var $d = $.Deferred();
setTimeout(function(){
console.log('t1');
$d.resolve();
},1000);
return $d.promise();
};
//2
var task2 = function(){
var $d = $.Deferred();
setTimeout(function(){
console.log('t2');
$d.resolve();
},1200);
return $d.promise();
};
//3
var task3 = function(){
var $d = $.Deferred();
setTimeout(function(){
console.log('t3');
$d.resolve();
},1200);
return $d.promise();
};
//4
var task4 = function(){
var $d = $.Deferred();
setTimeout(function(){
console.log('t4');
$d.resolve();
},1200);
return $d.promise();
};
var tasks = [task1,task2,task3,task4];
tasks.reduce(function(previous, p) {
previous().then(p());
$.Deferred().resolve();
});
// tasks.reduce((previous, p) => previous.then(p), $.Deferred().resolve());
</script>
</body>
</html>
就会提示这样的错误
想请教一下大家的看法,为什么会出错。
reduce
错了,reduce
有第二个参数。