I have a file, myClass.js which has a class with function in it.
class MyClass {
func1(param1, param2) {
return new Promise((resolve, reject) => {
resolve(param1+param2);
})
}
}
const myObj = new MyClass();
module.exports = myObj;
Then I call that function from another file and feed it through Promise.all.
const myObj = require('myClass.js')
let funcPromise1 = myObj.func1(1, 2);
let funcPromise2 = myObj.func1(2, 3);
let funcPromise3 = myObj.func1(3, 4);
Promise.all([funcPromise1, funcPromise2, funcPromise3])
.then(values=> {
console.log(values)
});
The problem is that the func1 gets invoked when I assign it to the variable. I could wrap it in another Promise, but that seems like an anti-pattern.
let funcPromise1 = new Promise((resolve, reject) => { myObj.func1(1, 2).then(sum=>{resolve(sum)}).catch(err=>{reject(err)});
I could just place the function in the promise all, but it starts to get hard to follow.
How can I not invoke this function immediately?