I'm starting to work with raw promises and async/await in Node.js.
I have 2 "promis-ified" functions that I want to run in parallel, and then()
perform some manipulation on the result and return the new data. The return value is always undefined
, but inside the .then()
the value is what I expect.
This is my function:
const fs = require('fs-promise-util').default;
/**
* compares 2 directories to find hooks of the same name
*
* @return {Array} hooks that exist in remote and local directories
*/
function remoteVSlocal () {
try {
Promise.all([
fs.readdir(REMOTE_HOOKS_PATH),
fs.readdir(LOCAL_HOOKS_PATH)
]).then(function ([REMOTE_HOOKS, LOCAL_HOOKS]) {
//filter out values that exist in both arrays
//this returns a new array with the values I expect
return LOCAL_HOOKS.filter(function (name) {
return REMOTE_HOOKS.includes(name);
});
});
} catch (err) {
return err;
}
}
When I call the function it returns undefined
:
console.log(remoteVSlocal());
I expect a call to remoteVSlocal()
to return the new array created by Array.filter()
.