24

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;
}
Rick
  • 4,030
  • 9
  • 24
  • 35
RockBoro
  • 2,163
  • 2
  • 18
  • 34

3 Answers3

26

The code inside the Promise constructor runs when the promise is created and it runs synchronously which surprises some people. So even without then() everything still runs.

new Promise(resolve => console.log("running"))

Code in the callback for the setTimeout however, doesn't run until it's called, but it too will run even without the then()

new Promise(resolve => {
  console.log("promise created")
  setTimeout(() => console.log("this runs later"), 1000)
})
Mark
  • 90,562
  • 7
  • 108
  • 148
  • but in my code I create the promise. Then run alert( ) to pause the code. The promise code does not run until after the alert. The promise code does not run until the promise objects are destroyed? Like there is a deconstructor? – RockBoro Jul 19 '18 at 20:49
  • @StephenRichter - yeah, I can see how that makes things confusing. `alert()` pauses timers so even though the promise runs (which you can confirm by putting a `console.log()` outside the `setTimeout`) the timers won't fire until you close the dialog box. – Mark Jul 19 '18 at 20:58
  • p.s. @StephenRichter here's a good thread on the timer issue: https://stackoverflow.com/questions/26487403/why-do-js-modal-message-boxes-pause-countdown-on-settimeout – Mark Jul 19 '18 at 20:59
  • 1
    @10101010 I think it answers the question that was asked. If you disagree, you should contribute an answer. – Mark Apr 03 '21 at 04:03
16

When calling .then you just set up a "callback" which says what should happen after the promise is done. So the promise will be called, with or without declaring such callback.

Imagine the promise that calls a AJAX request. Calling .then and passing a function to it will make it run when the ajax call was finished (successfully or not as in timeout or other errors). But not calling .then will not stop the request to be run. You will simply not react to the results of the request.

Greg
  • 5,862
  • 1
  • 25
  • 52
3

I thought, that "Promises won't run" if you don't call then/catch/finally on them, too. But if you consider, that these methods return new Promises, and according to your logic, you need to call then/catch/finally on these returned Promises in order to "run" them, you'll be stuck in an infinite loop)))

Veetaha
  • 833
  • 1
  • 8
  • 19