2

I am new to Node.js and am able to run these commands one by one one using promises:

let promise1 = new Promise(function (resolve, reject) {
sftp.connect({
    host: host,
    username: user,
    privateKey: fs.readFileSync(pemfile)
}).then(() => {
    return sftp.get(remotePath, fs.createWriteStream(localPath));   //This writes from a remote file to a local file
}).then(() => {
    sftp.end();
    resolve();
})
    .catch(err => {
        console.error(err.message);
        reject(err);
    });
});

await promise1;

let promise2 = new Promise(function (resolve, reject) {
    fs.readFile(localPath, 'utf8', function (err, data) {
        if (err) {
            reject(err);
        }
        resolve(data);
    });
});

let data  = await promise2;

This works but I know this is not the best way to do this. Is there a better way to do this?

Anshul Goyal
  • 119
  • 10
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Aug 10 '21 at 20:58

1 Answers1

5

await can be used directly with methods that return promise like sftp.connect, sftp.get, sftp.end so you can use it directly and the function will wait until the step is completed. Only fs.readfile does not return a Promise but we can call fs.promises.readFile() that returns promise then we can use await.

The code can be simplified:

try {

    await sftp.connect({
        host: host,
        username: user,
        privateKey: fs.readFileSync(pemfile)

    }) 

    await sftp.get(remotePath, fs.createWriteStream(localPath));   
    await sftp.end();
    let fileData = await fs.promises.readFile(localPath, 'utf8');
    console.log(fileData);

} catch (error) {
    console.error(error.message);
}

The try-catch block has been added to capture errors from any action.

MHassan
  • 415
  • 4
  • 9
  • 1
    Wouldn't it be better to use `fs.promises.readFile()` instead of manually promisifying `fs.readFile()`. – jfriend00 Aug 11 '21 at 02:47
  • @jfriend00 yes we can use another module "fs/promises" that export method readFile(fileName, { signal }) and this method return promise – MHassan Aug 11 '21 at 05:35
  • 1
    You can also use `fs.promises.readFile()` without importing another module. – jfriend00 Aug 11 '21 at 05:37
  • @jfriend00 you are right i edit the answer it becomes more simple. thank you too much – MHassan Aug 11 '21 at 16:20