How can I call series of Promise functions?

javascript promise w3schools
promise js
promise chaining
javascript promise then
async/await
return a promise from a function
javascript await
wait for promise to resolve

The requirement is finishing the current function before moving to the next call:

var current_data = something;
Run(current_data).then((data1) => {
    Run(data1).then(data2 => {
        Run(data2).then .....
    })
});

The example above is only possible if I know exactly how much data I want to get.

I'm gonna assume your data is paginated and you don't know how many pages there are, therefore you can use a while loop with await inside of an async function like so:

(async function() {
  var currentData = someInitialData;
  // loop will break after you've processed all the data
  while (currentData.hasMoreData()) {
    // get next bunch of data & set it as current
    currentData = await Run(currentData);
    // do some processing here or whatever
  }
})();

When to use promise.all()?, will follow that “then” to till the final state. resolutionFunc(value) // call on fulfilled rejectionFunc(reason) // call on rejected. The returned value can be another promise object, in which case the promise gets dynamically inserted into the chain. Return value promiseObj When called via new, the Promise constructor returns a promise object.

In order to make the nested promises part of promise chain, you need to return the nested promises.

Run(current_data).then((data1) => {
    return Run(data1).then(data2 => {
        return Run(data2).then .....
    });
});

How to execute promises sequentially, passing the parameters from , may be in one of 3 possible states: fulfilled, rejected, or pending. It causes the function to always return a promise whether or not you explicitly return something, so you can call then() on it for example. More on this later. More on this later. It allows you to use the await keyword inside it to wait on a promise until it is resolved before continuing on to the next line inside the async function.

You can use the async-await to make code more readable.

async function getData(current_data){
  let data1 = await Run(current_data)
  let data2 = await Run(data1);
  let result = await Run(data2);
  return result;  
}

Calling the getData function

getData(data)
 .then(response => console.log(response))
 .catch(error =>  console.log(error));

JavaScript, new Promise ( function ( resolve , reject ) { setTimeout ( ( ) => resolve ( 1 ) The whole thing works, because a call to promise.then returns a  As the result is passed along the chain of handlers, we can see a sequence of alert calls: 1 → 2 → 4. The whole thing works, because a call to promise.then returns a promise, so that we can call the next.then on it. When a handler returns a value, it becomes the result of that promise, so the next.then is called with it.

Try to avoid nested promises. If you need to call a series of promises, which depend on the previous call's response, then you should instead chain then like the following following -

const promise1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('foo');
  }, 1000);
});

promise1.then((response) => {

  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(response + ' b');
    }, 1000);
  });

}).then((responseB) => {


  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(responseB + ' c');
    }, 1000);
  });

}).then((responseC) => {

  console.log(responseC); // 'foo b c'
})

Master the JavaScript Interview: What is a Promise?, Each function is used by simply calling it when appropriate. resolutionFunc(value​) // call on fulfilled rejectionFunc(reason) // call on rejected. The  You should change each function to return a Promise, which will allow your final function to become: function runner() { return Promise.try(getData).then(parseData).then(validate); } To do that, the body of each function should be wrapped in a new promise, like:

If you are executing the same function over and over again but on different data, I would make a recursive function that returns return a Promise.

I just look at my example below using an an array of numbers, you can edit it to your current case.

var current_data = [1,2,4,5,6]

function Run(data){
  if(data.length === 0)
    return Promise.resolve(data);
  
  return new Promise((resolve, reject)=>{
    //your async operation
    //wait one second before resolving
    setTimeout(()=>{
      data.pop()
      console.log(data)
      resolve(data)
    },1000)
  })
  .then((results)=>{
    return Run(results)
  })
   
}

Run(current_data)

Promises chaining, JavaScript executes code in a single thread, which makes it blocking. Let's take a simple example of calling three functions in series. As we can  Get images. The displayimages() function takes an array of images and sequentially goes through each image, by calling images.shift().For each image, we first call getImage() to fetch the image, then the returned Promise object's then() method to specify what happens next, in this case, add the image to the doggyplayground DIV before calling displayimages() again.

Promise, You might run through the array expecting calls to run one-by-one. Let's imagine we need to make a series of asynchronous function calls. function asyncTimeout(delay) { return (new Promise(resolve => {setTimeout(()  Understand promises before you start using async/await. In order to call one of my async functions, ideally the caller itself should be an async function. args) {return new Promise

JavaScript Promises and Async/Await: As Fast As Possible™, of async/await functions (which return Promises), it can be tricky to figure out how to execute them all in series (one-at-a-time) and how to call  As specified in kba's answer: if you have an environment with native promises built-in (modern browser or node.js or using babeljs transpile or using a promise polyfill), then you can use ES6-specified promises. See this table for browser support. Promises are supported in pretty much all current browsers, except IE.

How to run async JavaScript functions in sequence or parallel, But when calling asynchronous functions in series, the data they 'return' is only available in the callback, so each call must be done in the callback of the  We can call .then on a Promise as many times as we want. Each time, we’re adding a new “fan”, a new subscribing function, to the “subscription list”. More about this in the next chapter: Promises chaining. There can be only one callback.

Comments
  • If my answer solves your problem please click the gray checkmark next to my answer to accept it, thank you!
  • What are differences between return and not?
  • If you do not add return in then callback, function creates the promises and return undefined. However, if you add return it returns the promise and make that promise part of the chain.