-
-
Save vpugh/1fa514802b3ef028d038477cbd5c0c83 to your computer and use it in GitHub Desktop.
Simple breakdown of functions and promises
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
| /* | |
| 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