Last active
May 23, 2016 20:53
-
-
Save daronwolff/6fb66f5f2acda64d993b0d2e8ff27790 to your computer and use it in GitHub Desktop.
Promises Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var restante = 0; | |
'use strict'; | |
function wait() { | |
return new Promise(function(done, reject) { | |
setTimeout(function() { | |
if (restante > 0) { | |
done(); | |
} else { | |
reject(); | |
} | |
}, 2000); | |
}); | |
} | |
wait(). | |
then(function() { | |
$("h1").text("Ok,Preparando expresso..."); | |
return wait(); | |
}). | |
then(function() { | |
$("h1").text("Listo, tenga su expresos"); | |
return wait(); | |
}). | |
then(function() { | |
restante -= 1; | |
$("h1").text("Gracias :)"); | |
}). | |
catch(function() { | |
$("h1").text("Lo lamento, no hay expreso :("); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DEMO
https://jsfiddle.net/daronwolff/n10n17ag/