Last active
October 10, 2016 05:02
-
-
Save jessemoon0/aa7944e949635ecc33e6e876e73a95b4 to your computer and use it in GitHub Desktop.
First Example of a Javascript Promise (Tutorial)
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
//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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To see results: Check in JSFiddle and open the dev tools console.