0

So I have a bunch of mysql calls I'm making that I'm looking to make the code a little cleaner and more re-usable by separating the logic out into their own functions and what I want to do, since the number of calls can always grow, is build a promise array and execute them via Promise.all. I'm already familiar with this concept, but usually my approach would be something like this:

const promiseChain = []

array.forEach(item => promiseChain.push(new Promise((resolve, reject) { ... logic }); 

Or something similar to this. But I can't quit do it this way because I want to seperate out all the logic, so now I have a bunch of different functions, all returning promises. I'm a little confused on what syntax I can use to now just add the functions to the array without actually calling them. I did see one SO answer which specified something like this:

const array = [func1, func2]

Although I don't think this works, bc my functions would all have different parameters. Is there a good, clean way to add functions that return promises to an array (obviously without executing them) that also receive parameters? Or would it just be something like this:

promiseChain.push(new Promise((resolve, reject) => {
functionPromise(params).then(() => resolve()).catch(err => reject(err))
})

I guess what I'm asking is, is there a better way to do it then the above? Just seems like more code than necessary since those functions already return promises. Seems like overkill.

3
  • You definitely don't need to construct new Promises when your functions already return Promises, but I'm not sure exactly what you're trying to do. Promise.all doesn't make the Promises resolve in series, it just waits for all of them to resolve, so you may as well call you functions when creating/filling the array. Just do const promises = [func1(params1), func2(params2)]; or promises.push( func3( params3 ) ); Commented Aug 12, 2019 at 17:52
  • If you want to execute them in series then Promise.all isn't the right thing to use (since you're only ever waiting for one Promise to resolve). You should do something like func1(params1).then( _ => func2(params2) ).then( _ => func3( params3 ) ) or await func1( params1 ); await func2( params2 ); await func3( params3 ) Commented Aug 12, 2019 at 17:54
  • ok I think that makes sense, I guess I didn't think about it that way. Will try that. Commented Aug 12, 2019 at 17:54

1 Answer 1

1

is there a better way to do it then the above?

Your example is equivelent to: promiseChain.push(functionPromise(params));

Is there a good, clean way to add functions that return promises to an array (obviously without executing them) that also receive parameters

To add functions without executing them: array.push(functionPromise);

The param can be bound to the functions in your array:

array.push(funcitonPromise.bind(funcitonPromise, ...params))

Then they can be invoked later by:

await Promise.all(array.map(func => func()));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.