Skip to content

Instantly share code, notes, and snippets.

@optimistanoop
Created November 6, 2016 09:13
Show Gist options
  • Save optimistanoop/8b85008ad7d47849004b884344550fd1 to your computer and use it in GitHub Desktop.
Save optimistanoop/8b85008ad7d47849004b884344550fd1 to your computer and use it in GitHub Desktop.
Js Promise chaining
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);
}
@optimistanoop
Copy link
Author

optimistanoop commented Nov 6, 2016

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.

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