Jump to content
Sign in to follow this  
kaotkbliss

NodeJS promise function chaining

Recommended Posts

I have to functions, each return a promise. I need to execute the first function 2x, then when it's done, execute the 2nd function 2x

currently it's set up in this format

async function somename (parameters) {
  promise = new Promise((resolve, reject)=> {
    do stuff;
  };
  complete = await promise;
};

async function somename2 (parameters) {
  promise = new Promise((resolve, reject)=> {
    do stuff;
  };
  complete = await promise;
};

somename(parameters).then((onReturn) => {
  somename(newparameters).then((onReturn) => {
    somename2(parameters).then((onReturn) => {
      somename2(newparameters).then((onReturn) => {
        do stuff;
      })
    })
  })
})

Ideally I'd like to just call the first function 2x, then wait for both to finish, then call the next one 2x and wait for both of those to finish. That way each pair of functions is running at the same time.


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

Managed to figure this out :)

async function somename (parameters) {
  promise = new Promise((resolve, reject)=> {
    do stuff;
    resolve (return val);
  };
  complete = await promise;
};

async function somename2 (parameters) {
  promise = new Promise((resolve, reject)=> {
    do stuff;
    resolve (return val);
  };
  complete = await promise;
};

let getSomename = async (parameters, newParameters) => {
  await somename(parameters);
  await somename(newParameters);
  return (some val);
};

let getSomename2 = async (parameters, newParameters) => {
  await somename2(parameters);
  await somename2(newParameters);
  return (some val);
};

getSomename(parameters, newParameters).then((onReturn) => {
  getSomename2(parameters, newparameters).then((onReturn) => {
    do stuff;
  })
})

 


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...