I create two promises, but I do not run the then method on those promises. Yet once the promise objects go out of scope, the promise code runs as if .then
was called.
How is a Promise
settled without a call to the .then
method?
I am asking because I would like to load an array with Promise
objects and then run the promises in sequence.
function promises_createThenRun() {
const p1 = createPromise1();
const p2 = createPromise2();
console.log('before hello');
alert('hello');
console.log('after hello');
// the two promises run at this point. What makes them run?
}
function createPromise1() {
let p1 = new Promise((resolve, reject) => {
window.setTimeout(() => {
console.log('timer 1');
resolve();
}, 2000);
});
return p1;
}
function createPromise2() {
let p2 = new Promise((resolve, reject) => {
window.setTimeout(() => {
console.log('timer 2');
resolve();
}, 1000);
});
return p2;
}