Skip to content

Instantly share code, notes, and snippets.

@jessemoon0
Last active October 10, 2016 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessemoon0/aa7944e949635ecc33e6e876e73a95b4 to your computer and use it in GitHub Desktop.
Save jessemoon0/aa7944e949635ecc33e6e876e73a95b4 to your computer and use it in GitHub Desktop.
First Example of a Javascript Promise (Tutorial)
//First code about promises
let promiseToCleanTheRoom = new Promise(function(resolve, reject){
//Resolve: Means I am fullfilling this promise (resolving it).
//Reject: Promise is not Fullfilled in given time or constraint.
//HERE IS CLEANING THE ROOM CODE....
//After doing this, clean's value gets set.
let isClean = true; //Here you control resolve and reject in this example.
if(isClean){
resolve('Clean');
} else {
reject('Not Clean');
}
});
//This fires when promise is resolved
promiseToCleanTheRoom.then(function(fromResolve){
//fromResolve receives argument inside the resolve function (in this case 'Clean')
console.log('The room is ' + fromResolve);
}).catch(function(fromReject){
//same applies to fromReject
console.log('The room is ' + fromReject);
});
@jessemoon0
Copy link
Author

To see results: Check in JSFiddle and open the dev tools console.

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