bluebird
Version:
Full featured Promises/A+ implementation with exceptionally good performance
53 lines (50 loc) • 2.02 kB
JavaScript
/**
* Copyright (c) 2013 Petka Antonov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:</p>
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
;
var ensureNotHandled = require( "./errors.js" ).ensureNotHandled;
var util = require( "./util.js");
var tryCatch1 = util.tryCatch1;
var errorObj = util.errorObj;
function CatchFilter( instances, callback, boundTo ) {
this._instances = instances;
this._callback = callback;
this._boundTo = boundTo;
}
CatchFilter.prototype.doFilter = function CatchFilter$doFilter( e ) {
if( e === null || typeof e !== "object" ) {
throw e;
}
var cb = this._callback;
for( var i = 0, len = this._instances.length; i < len; ++i ) {
var item = this._instances[i];
if( e instanceof item ) {
var ret = tryCatch1( cb, this._boundTo, e );
if( ret === errorObj ) {
throw ret.e;
}
return ret;
}
}
ensureNotHandled( e );
throw e;
};
module.exports = CatchFilter;