js promise chain code example
Example 1: javascript promise chain
promise
.then(...)
.then(...)
.then(...)
.catch(...)
coinflip(10)
.then(betAgain)
.then(betAgain)
.then(betAgain)
.then(result => {
console.log(`OMG, WE DID THIS! TIME TO TAKE ${result} HOME!`);
})
.catch(handleRejection);
Example 2: how to implement a promise with daisy chaining in angular
private firstAction():Promise<any> {
return new Promise<any>(
(resolve, reject) => { ... }
);
}
private secondAction():Promise<any> {
return new Promise<any>(
(resolve, reject) => { ... }
);
}
execute() {
this.firstAction().then(
(firstResult:any) => this.secondAction().then(
(secondResult:any) => { ... }
);
)
}
Example 3: promise chaining
new Promise(function(resolve, reject) {
setTimeout(() => resolve(1), 1000);
}).then(function(result) {
alert(result);
return result * 2;
}).then(function(result) {
alert(result);
return result * 2;
}).then(function(result) {
alert(result);
return result * 2;
});