Skip to content

Instantly share code, notes, and snippets.

@zerotacg
Created March 6, 2015 19:19
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zerotacg/d304348cdd5d2f965901 to your computer and use it in GitHub Desktop.
function request( delay, value, fail )
{
return new Promise(function( fullfill, reject ) {
setTimeout( (fail ? reject : fullfill).bind( this, value), delay );
});
}
var render = console.log.bind( console, "render" )
, error = console.log.bind( console, "error" )
;
function failBy( rejected, rejectee )
{
return (rejected
.catch( function( error ) {
return rejectee.catch( Promise.reject.bind( Promise, error ) );
})
);
}
function rejectBy( rejected, rejectee )
{
return new Promise( function( fullfill, reject )
{
rejected.then( fullfill, reject );
rejectee.then( reject );
});
}
var slow = 500, fast = 1, fail = true, succeed = false;
var network = request( slow, "network", succeed )
, cache = request( fast, "cache", succeed )
;
failBy( network, cache ).then( render ).catch( error );
rejectBy( cache, network ).then( render );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment