Comparing version 0.4.2 to 0.4.3
@@ -61,6 +61,14 @@ 'use strict'; | ||
* | ||
* The rejection array comes extended with the following: | ||
* - function `getErrors()` - returns the complete list of errors only, with support for nested `batch` results | ||
* - property `first` - returns the very first error within the array, with support for nested `batch` results, | ||
* so it is not the same as simply calling `getErrors()[0]` | ||
* - property `message` is a safe version of `first.message` | ||
* | ||
* In addition, the rejection array is extended with function `getErrors`, which returns the list of just | ||
* errors, with support for nested batch results. Calling `getErrors()[0]`, for example, will get the same | ||
* result as the rejection reason that `promise.all` would provide. | ||
* result as the rejection reason that `promise.all` would provide (unless it is a nested `batch` result). | ||
* | ||
* The rejection array is also extended with property `message` that returns | ||
* | ||
* In all cases, the output array is always the same size as the input one, this way providing index mapping | ||
@@ -75,3 +83,3 @@ * between the input values and the results. | ||
var $p = config.promise, $utils = config.utils; | ||
if (!values.length) { | ||
@@ -155,8 +163,15 @@ var empty = []; | ||
err[i] = result[errors[i]].result; | ||
if (err[i] instanceof Array && err[i].getErrors instanceof Function) { | ||
if (Array.isArray(err[i]) && err[i].getErrors instanceof Function) { | ||
err[i] = err[i].getErrors(); | ||
} | ||
} | ||
$utils.extend(err, 'isErrorArray', true); | ||
return err; | ||
}); | ||
var err = result.getErrors(); | ||
while (err && err.isErrorArray) { | ||
err = err[0]; | ||
} | ||
$utils.extend(result, 'first', err); | ||
$utils.extend(result, 'message', (err && err.message) || err); | ||
reject(result); | ||
@@ -163,0 +178,0 @@ } else { |
@@ -89,3 +89,3 @@ 'use strict'; | ||
* Object for both reject types has method `getError()` to simplify access to the error. | ||
* For *Normal Rejects* it will return `data.getErrors()[0]` (see method $[batch]), | ||
* For *Normal Rejects* it will return `data.first` (see method $[batch]), | ||
* and `error` value for the *Internal Rejects*. | ||
@@ -189,3 +189,3 @@ */ | ||
$utils.extend(reason, 'getError', function () { | ||
return ('data' in reason) ? reason.data.getErrors()[0] : reason.error; | ||
return ('data' in reason) ? reason.data.first : reason.error; | ||
}); | ||
@@ -192,0 +192,0 @@ reject(reason); |
{ | ||
"name": "spex", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "Specialized Promise Extensions", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -110,3 +110,3 @@ 'use strict'; | ||
function value() { | ||
throw msg; | ||
throw new Error(msg); | ||
} | ||
@@ -125,8 +125,62 @@ | ||
success: false, | ||
result: msg | ||
result: new Error(msg) | ||
} | ||
]); | ||
expect(error.first).toEqual(new Error(msg)); | ||
expect(error.message).toBe(msg); | ||
}); | ||
}); | ||
describe("input: null reject", function () { | ||
var error; | ||
function value() { | ||
throw null; | ||
} | ||
beforeEach(function (done) { | ||
spex.batch([value]) | ||
.catch(function (reason) { | ||
error = reason; | ||
done(); | ||
}) | ||
}); | ||
it("must reject correctly", function () { | ||
expect(error).toEqual([ | ||
{ | ||
success: false, | ||
result: null | ||
} | ||
]); | ||
expect(error.first).toBe(null); | ||
expect(error.message).toBe(null); | ||
}); | ||
}); | ||
describe("input: simple reject", function () { | ||
var error; | ||
function value() { | ||
throw 123; | ||
} | ||
beforeEach(function (done) { | ||
spex.batch([value]) | ||
.catch(function (reason) { | ||
error = reason; | ||
done(); | ||
}) | ||
}); | ||
it("must reject correctly", function () { | ||
expect(error).toEqual([ | ||
{ | ||
success: false, | ||
result: 123 | ||
} | ||
]); | ||
expect(error.first).toBe(123); | ||
expect(error.message).toBe(123); | ||
}); | ||
}); | ||
describe("nested batch reject", function () { | ||
@@ -151,4 +205,6 @@ var error, msg = "internal failure"; | ||
it("must be reported correctly", function () { | ||
expect(error).toEqual([{success: false, result: [{success: false, result: 'internal failure'}]}]); | ||
expect(error).toEqual([{success: false, result: [{success: false, result: msg}]}]); | ||
expect(error.getErrors()).toEqual([[msg]]); | ||
expect(error.first).toBe(msg); | ||
expect(error.message).toBe(msg); | ||
}); | ||
@@ -155,0 +211,0 @@ }); |
@@ -156,3 +156,3 @@ 'use strict'; | ||
if (idx > 1) { | ||
return [1, promise.reject(2), 3]; | ||
return [1, promise.reject('second'), 3]; | ||
} | ||
@@ -180,3 +180,3 @@ return []; | ||
success: false, | ||
result: 2 | ||
result: 'second' | ||
}, | ||
@@ -189,3 +189,3 @@ { | ||
}); | ||
expect(error.getError()).toBe(2); | ||
expect(error.getError()).toBe('second'); | ||
}); | ||
@@ -192,0 +192,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76896
2069
20
1
0