多重promise(串行)
let test = function() {
return new Promise(function(resolve, reject){
setTimeout(function(){
resolve();
}, 2000);
})
}
test().then(function() {
console.log('測(cè)試 resolve')
})
test().then(function(){
return new Promise(function(resolve, reject){
setTimeout(function(){
resolve()
}, 2000);
})
}).then(function(){
console.log('多重promise')
})
輸出的結(jié)果
node .\promis-test.js
測(cè)試 resolve
多重promise
promise中的catch
// 可以拋出一個(gè)錯(cuò)誤抵怎,然后用promise中的catch接住
// promise 中的 catch 用于捕獲異常
let test = function(num) {
return new Promise(function(resolve, rejcet){
if (num > 5) {
resolve();
} else {
// 拋出一個(gè)錯(cuò)誤
throw new Error;
}
})
}
test(3).then(function() {
console.log('resolve 正常');
}).catch(function(error) {
console.log('捕獲異常', error);
})