bluebird
Version:
Full featured Promises/A+ implementation with exceptionally good performance
220 lines (195 loc) • 5.88 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 global = require("./global.js");
var ASSERT = require("./assert.js");
var haveGetters = (function(){
try {
var o = {};
Object.defineProperty(o, "f", {
get: function () {
return 3;
}
});
return o.f === 3;
}
catch(e) {
return false;
}
})();
var ensurePropertyExpansion = function( obj, prop, value ) {
try {
notEnumerableProp( obj, prop, value );
return obj;
}
catch( e ) {
var ret = {};
var keys = Object.keys( obj );
for( var i = 0, len = keys.length; i < len; ++i ) {
try {
var key = keys[i];
ret[key] = obj[key];
}
catch( err ) {
ret[key] = err;
}
}
notEnumerableProp( ret, prop, value );
return ret;
}
};
var canEvaluate = (function() {
if( typeof window !== "undefined" && window !== null &&
typeof window.document !== "undefined" &&
typeof navigator !== "undefined" && navigator !== null &&
typeof navigator.appName === "string" &&
window === global ) {
return false;
}
return true;
})();
function deprecated( msg ) {
if( typeof console !== "undefined" && console !== null &&
typeof console.warn === "function" ) {
console.warn( "Bluebird: " + msg );
}
}
var isArray = Array.isArray || function( obj ) {
return obj instanceof Array;
};
var errorObj = {e: {}};
function tryCatch1( fn, receiver, arg ) {
try {
return fn.call( receiver, arg );
}
catch( e ) {
errorObj.e = e;
return errorObj;
}
}
function tryCatch2( fn, receiver, arg, arg2 ) {
try {
return fn.call( receiver, arg, arg2 );
}
catch( e ) {
errorObj.e = e;
return errorObj;
}
}
function tryCatchApply( fn, args, receiver ) {
try {
return fn.apply( receiver, args );
}
catch( e ) {
errorObj.e = e;
return errorObj;
}
}
var inherits = function( Child, Parent ) {
var hasProp = {}.hasOwnProperty;
function T() {
this.constructor = Child;
this.constructor$ = Parent;
for (var propertyName in Parent.prototype) {
if (hasProp.call( Parent.prototype, propertyName) &&
propertyName.charAt(propertyName.length-1) !== "$"
) {
this[ propertyName + "$"] = Parent.prototype[propertyName];
}
}
}
T.prototype = Parent.prototype;
Child.prototype = new T();
return Child.prototype;
};
function asString( val ) {
return typeof val === "string" ? val : ( "" + val );
}
function isPrimitive( val ) {
return val == null || val === true || val === false ||
typeof val === "string" || typeof val === "number";
}
function isObject( value ) {
return !isPrimitive( value );
}
function maybeWrapAsError( maybeError ) {
if( !isPrimitive( maybeError ) ) return maybeError;
return new Error( asString( maybeError ) );
}
function nodebackForResolver( resolver ) {
function PromiseResolver$_callback( err, value ) {
if( err ) {
resolver.reject( maybeWrapAsError( err ) );
}
else {
if( arguments.length > 2 ) {
var len = arguments.length;
var val = new Array( len - 1 );
for( var i = 1; i < len; ++i ) {
val[ i - 1 ] = arguments[ i ];
}
value = val;
}
resolver.fulfill( value );
}
}
return PromiseResolver$_callback;
}
function withAppended( target, appendee ) {
var len = target.length;
var ret = new Array( len + 1 );
var i;
for( i = 0; i < len; ++i ) {
ret[ i ] = target[ i ];
}
ret[ i ] = appendee;
return ret;
}
function notEnumerableProp( obj, name, value ) {
var descriptor = {
value: value,
configurable: true,
enumerable: false,
writable: true
};
Object.defineProperty( obj, name, descriptor );
return obj;
}
module.exports ={
isArray: isArray,
haveGetters: haveGetters,
notEnumerableProp: notEnumerableProp,
isPrimitive: isPrimitive,
isObject: isObject,
ensurePropertyExpansion: ensurePropertyExpansion,
canEvaluate: canEvaluate,
deprecated: deprecated,
errorObj: errorObj,
tryCatch1: tryCatch1,
tryCatch2: tryCatch2,
tryCatchApply: tryCatchApply,
inherits: inherits,
withAppended: withAppended,
asString: asString,
maybeWrapAsError: maybeWrapAsError,
nodebackForResolver: nodebackForResolver
};