Skip to content

Instantly share code, notes, and snippets.

@vpugh
Created January 23, 2019 20:18
Show Gist options
  • Select an option

  • Save vpugh/1fa514802b3ef028d038477cbd5c0c83 to your computer and use it in GitHub Desktop.

Select an option

Save vpugh/1fa514802b3ef028d038477cbd5c0c83 to your computer and use it in GitHub Desktop.
Simple breakdown of functions and promises
/*
var shouldIResolve = false;
function explainPromises() {
return new Promise(
function(resolve, reject) {
if (shouldIResolve === true) {
console.log("About to resolve...");
resolve();
} else {
console.log("About to reject");
reject();
}
});
}
explainPromises()
.then(function() { console.log('prmoise is done resolving.'); } )
.catch(function() { console.log('promise is done rejecting.'); } );
*/
// ====================================================================================
function runAnimation(step) {
console.log(step);
}
function delay(interval) {
return new Promise(function(resolve, reject) {
if (interval === 1000) {
setTimeout(resolve, interval);
}
else {
reject();
}
});
}
// chained promises
runAnimation(0);
delay(1000)
.then(function() {
runAnimation(1);
return delay(200).then( () => { console.log('Delay of 200 passed??'); } ).catch( () => { console.log('Delay of 200 failed!') } );
})
.catch(function() {
console.log("Delay of 1000 failed?");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment