Paste P7788

ES6 Deferred
ActivePublic

Authored by Esanders on Sun, Nov 11, 3:47 PM.
ve.createDeferred = function () {
var deferred = {},
promise = new Promise( function ( resolve, reject ) {
deferred.resolve = function ( value ) {
if ( arguments.length > 1 ) {
ve.log( 'ES6 Promises can\'t be resolved with more than one argument' );
}
resolve.call( this, value );
return deferred;
};
deferred.reject = function ( value ) {
if ( arguments.length > 1 ) {
ve.log( 'ES6 Promises can\'t be rejected with more than one argument' );
}
reject.call( this, value );
return deferred;
};
} );
promise.done = function ( f ) {
return promise.then( f );
};
promise.fail = function ( f ) {
return promise.then( null, f );
};
deferred.then = promise.then;
deferred.always = promise.finally;
deferred.promise = function () {
return promise;
};
return deferred;
};