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.
const promises = [func1(params1), func2(params2)];orpromises.push( func3( params3 ) );Promise.allisn't the right thing to use (since you're only ever waiting for one Promise to resolve). You should do something likefunc1(params1).then( _ => func2(params2) ).then( _ => func3( params3 ) )orawait func1( params1 ); await func2( params2 ); await func3( params3 )