Skip to content

Instantly share code, notes, and snippets.

@LarryBattleWork
Created February 10, 2016 22:32
Show Gist options
  • Save LarryBattleWork/d7683fa17b7ca1d04a98 to your computer and use it in GitHub Desktop.
Save LarryBattleWork/d7683fa17b7ca1d04a98 to your computer and use it in GitHub Desktop.
Simple Promise Example
var thinkForXMin = function(time, out){
return new Promise(
function(t, c){
setTimeout(function(){
t(out);
}, time);
}
);
};
var checkLot = function(t){
setTimeout(function(){
//c("I LOST");
t( Promise.reject("I lost big time. :-(") );
}, 1000);
};
var aaron = function(result){
console.log( "Aaron Message: " + result );
return "Aaron going back to work.";
};
var larry = function(result){
console.log( "Larry Message: " + result );
return thinkForXMin(1000, "I only want to buy if you lose.");
};
var whatWouldHeDo = function(result){
console.log( "He did this. Message: " + result );
};
var catchEverything = function(result){
console.log( "1) WTF is this? " + result );
return "1) Stop playing the lottery.";
};
var catchEverything2 = function(result){
console.log( "2) WTF is this? " + result );
return "2) Stop playing the lottery.";
};
var p = new Promise(checkLot);
p.catch(larry);
p.then(aaronm).then(whatWouldHeDo).catch(catchEverything);
p.catch(catchEverything2).then(larry).then(whatWouldHeDo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment