Created
November 6, 2016 09:13
-
-
Save optimistanoop/8b85008ad7d47849004b884344550fd1 to your computer and use it in GitHub Desktop.
Js Promise chaining
This file contains 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 p = new Promise(function(resolve, reject){ | |
var name = 'anoop'; | |
resolve(name); | |
var name = 'error' | |
//reject(name); | |
}) | |
p.then(addLastName) | |
.catch( handle); | |
function addLastName(name){ | |
// console.log(name+'rai'); | |
//return name+'rai' | |
var p = new Promise(function(resolve, reject){ | |
var name = 'anoop'; | |
//resolve(name); | |
var name = 'error' | |
reject(name); | |
}) | |
return p; | |
} | |
function showName(name){ | |
console.log(name); | |
} | |
function handle(name){ | |
console.log(name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Promises are different then events in these ways-
1- They are called only once.
2- we can have simple chaining then nested and complex chaining.
3- They are wrapped in try catch block
4- If a promise return any value , that itself becomes then-able.
5- Promise can be handled once it is already settled unlike eventHandlers we can't handle an event before setting eventHandlers.