Skip to content

Instantly share code, notes, and snippets.

@daronwolff
Last active May 23, 2016 20:53
Show Gist options
  • Save daronwolff/6fb66f5f2acda64d993b0d2e8ff27790 to your computer and use it in GitHub Desktop.
Save daronwolff/6fb66f5f2acda64d993b0d2e8ff27790 to your computer and use it in GitHub Desktop.
Promises Example
(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 :(");
});
})();
@daronwolff
Copy link
Author

daronwolff commented May 23, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment