2017年1月24日 星期二

(js)Promise不能捕捉到的錯誤

//var Promise = require('../lib/promise');


var p1 = new Promise((resolve, reject) => {
    setTimeout(resolve, 1000, "one");
});
var p2 = new Promise((resolve, reject) => {
    setTimeout(resolve, 2000, "two");
});
var p3 = new Promise((resolve, reject) => {
    setTimeout(resolve, 3000, "three");
});
var p4 = new Promise((resolve, reject) => {
    setTimeout(function() {
        resolve("four");
    }, 4000);
});

var p5 = new Promise((resolve, reject) => {
    setTimeout(function() {
        reject("p5 reject");
    }, 5000);
});

var p6 = new Promise((resolve, reject) => {
    setTimeout(resolve, 4000, "six");
});

/*
 * Promise.all([p1, p2, p3, p4]).then(values => { console.log(values); }, reason => {
 * console.log(reason) });
 */

Promise.all([p1, p2, p3, p4, p6]).then(values => {
    console.log(values);
}).catch(reason => {
    console.log(reason)
});