I've already searched among a lot of questions asked by other users but I can't still figure out why my function doesn't work properly. I wrote this code:
async function getFile(c, sourcePath, fileName, destPath) {
return await new Promise((resolve, reject) => {
c.cwd(sourcePath, function(err, currentDir) { // "c" is an instance of ftp module's class, "cwd" is one of its methods to change working directory
err = new Error('trying to throw an error for testing the func');
if (err) reject(err);
c.get(fileName, function(err, stream) {
if (err) reject(err);
stream.once('close', function() { resolve(); });
stream.pipe(fs.createWriteStream(destPath));
});
});
}));
}
(async function() {
try {
await getFile(c, '/path/to', 'file.xls', 'file.xls');
} catch (err) {
throw err;
}
})();
Node returns an UnhandledPromiseRejectionWarning, and I don't understand why. I have returned a promise in the first function and then called that function in another one correctly structured with async-await syntax. What is wrong with my code?