lib/plugins/promise/allAnyway.js   A
last analyzed

Complexity

Total Complexity 6
Complexity/F 1.5

Size

Lines of Code 35
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 35
rs 10
wmc 6
mnd 1
bc 6
fnc 4
bpm 1.5
cpm 1.5
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B allAnyway.js ➔ ??? 0 25 2
1
"use strict";
2
3
/**
4
 * @param {Array<callback>} callbackList
5
 *
6
 * @returns {Promise<Object>} Return a promise resolved even if one or more callbacks have failed
7
 *                            Resolved value is an object containing resolvedList and rejectedList
8
 *                            - resolvedList will contains successful callback returned value ordered by callback index
9
 *                            - rejectedList will contains rejected callback error ordered by callback index
10
 */
11
module.exports = (callbackList) => {
12
    const final = {
13
        resolvedList: [],
14
        rejectedList: []
15
    };
16
17
    if (callbackList.length === 0) {
18
        return Promise.resolve(final);
19
    }
20
21
    const mapCallback = (callback, index) => {
22
        return new Promise(resolve => {
23
            try {
24
                final.resolvedList[index] = callback();
25
                resolve();
26
            } catch (e) {
27
                final.rejectedList[index] = e;
28
                resolve();
29
            }
30
        });
31
    };
32
33
    return Promise.all(callbackList.map(mapCallback))
34
        .then(() => final);
35
};
36