doodlewind / nativebird

🐦 Bluebird alternative within ~200 loc
78 stars 9 forks source link

Promise.mapSeries 异常不会被 catch 捕获 #5

Closed a573367014 closed 1 year ago

a573367014 commented 1 year ago
Promise.mapSeries([0, 1, 2], (v) => {
    if (v > 0) throw new Error('111');
    return v;
}).catch(() => {
    // 没触发
    debugger;
});
a573367014 commented 1 year ago
// TODO refactor with Symbol.Iterable
Promise.mapSeries = function mapSeries(arr, fn) {
    if (!Array.isArray(arr)) {
        throw new TypeError(`Promise.mapSeries requires array, but got ${typeof arr}`);
    }
    return new Promise(async (resolve) => {
       // 看起来是这里没加 reject 导致
        const results = [];
            for (let i = 0; i < arr.length; i++) {
                const val = await Promise.resolve(arr[i]);
                results[i] = await fn(val, i, arr.length);
            }
        resolve(results);
    });
};
doodlewind commented 1 year ago

PR welcome :D

doodlewind commented 1 year ago

fixed in nativebird@1.2.9