-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
34 lines (32 loc) · 906 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var P = require('bluebird');
var ObjectPromiseSolver = require('./lib/ObjectPromiseSolver.js');
var PromiseSolver = require('./lib/PromiseSolver.js');
/**
* Choses which type of resolver to instantiate.
*
* @param {Object} promise
* @returns {Object} an instance of a resolver
*/
function getPromiseSolver(promise) {
return promise && promise.p ? new ObjectPromiseSolver() : new PromiseSolver();
}
/**
* Main exporting function
*
* @param {Array} promises
* @returns {Array}
*/
module.exports = function(promises) {
return new P(function(resolve, reject) {
var promiseSolver = getPromiseSolver(promises[0]);
promiseSolver.all(promises).then(
function(res) {
var cleanResponse = promiseSolver.clearResponse(res);
if (promiseSolver.hasErrors(res)) {
return reject(cleanResponse);
}
return resolve(cleanResponse)
}
);
});
};