Note: I'm assuming they're nested for a reason. Otherwise, use Promise.all
to run them in parallel and add up the array you receive when it resolves.
If I assume your code really is largely as shown, that the .
s don't introduce too much complexity, there are a couple of ways, but probably the simplest in that example is to just nest the promise handlers:
someFile1.someFunction1(req)
.then((result1) => {
return someFile2.someFunction2(req)
.then((result2) => {
return result1 + result2;
});
})
.then(combined => {
// Use the combined result
});
or with concise arrow functions:
someFile1.someFunction1(req)
.then(result1 =>
someFile2.someFunction2(req).then(result2 => result1 + result2)
)
.then(combined => {
// Use the combined result
});
or of course, use an async
function and await
:
const result1 = await someFile1.someFunction1(req);
const result2 = await someFile2.someFunction2(req);
const combined = result1 + result2;
or even
const combined = (await someFile1.someFunction1(req)) + (await someFile2.someFunction2(req));