How does Promise run when .then method is not called?

promise.then is not a function
promise then not called
promise then return value
promise((resolve is not a function))
promise is not a function
wait for promise to resolve
javascript nested promises
calling a function that returns a promise

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;
}

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"))

Promise.prototype.then(), You can schedule that second function by calling .then() and passing in the function. Each call to .then() waits for the previous promise, runs the next function, then reject(new Error('Could not load image at ' + url)); }; }); If one or both arguments are omitted or are provided non-functions, then then will be missing the handler(s), but will not generate any errors. If the Promise that then is called on adopts a state (fulfillment or rejection) for which then has no handler, a new Promise is created with no additional handlers, simply adopting the final state of the original Promise on which then was called.

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.

Using Promises, As we can see from the above result, each function call and… Using Async/ Await syntax, a promise-based asynchronous code can They are not included in the JavaScript engine. Then the callback function gets executed. Its job is to accept callback functions and execute them on the main thread. When the promise is settled, i.e either fulfilled or rejected, the specified callback function is executed. This provides a way for code to be run whether the promise was fulfilled successfully or rejected once the Promise has been dealt with. This helps to avoid duplicating code in both the promise's then() and catch() handlers. Syntax

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)))

Promises chaining, We all know we can make the async call as below But I am sure… But I am sure some beginners do not know how Promise works and what All promise instances have then method which is used to add two callbacks : onResolved and onRejected . You can use Promise.all to run multiple operations. To be precise, a handler may return not exactly a promise, but a so-called “thenable” object – an arbitrary object that has a method .then. It will be treated the same way as a promise. The idea is that 3rd-party libraries may implement “promise-compatible” objects of their own.

Promise, If you chain asynchronous function calls via then() , they are executed sequentially, No inversion of control: similarly to synchronous code, Promise- based functions That means that your code can rely on run-to-completion semantics (as� The Promise.all () method returns a single Promise that fulfills when all of the promises passed as an iterable have been fulfilled or when the iterable contains no promises or when the iterable contains promises that have been fulfilled and non-promises that have been returned.

Working with Promises | Web, In the second example, you are not calling catch on the original promise, you call catch on the promise returned by then . If a then has no error callback provided, it � The Promise.resolve() method returns a Promise object that is resolved with a given value. If the value is a promise, that promise is returned; if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value. This function flattens nested layers of promise-like

JavaScript Promises and Async/Await: As Fast As Possible™, The function to be called if the promise is fulfilled successfully with a value. The fulfilled value is passed as the single argument. If the value is null, the fulfilled value is returned. The value returned from the function becomes the fulfilled value of the promise returned by then.

Comments
  • Why would you think that the setTimeout() callbacks would not be called?
  • Just because you aren't using .then doesn't mean the function won't continue to execute.
  • asking when are the setTimeout callbacks called. They appear to be called when the promise goes out of scope.
  • 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?
  • @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.
  • p.s. @StephenRichter here's a good thread on the timer issue: stackoverflow.com/questions/26487403/…