Chaining Promises
You can chain promises by using multiple then functions
- the then(n) will wait until the first asyncronous task completes before executing
//Promises in ES6 - Basic Setup - Chaining Promises
function waitToResolve(seconds) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
seconds++;
resolve(seconds);
}, 1000);
});
}
waitToResolve(0)
.then(waitToResolve)
.then(function(seconds) {
console.log(seconds)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18