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?
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.
Related
This question already has answers here:
How do I convert an existing callback API to promises?
(24 answers)
Closed 2 years ago.
I have the following code on a server:
let tmpFileName;
// GET
app.get('/clicked', async (req, res) => {
let nullOutput = writeTmpFile("hello, world!");
await deleteTmpFile();
console.log("Hurray, finished!");
res.send({result:nullOutput});
})
function writeTmpFile(content){
tmpFileName = "tmp" + Math.random().toString() + "tsl";
return new Promise(resolve => {
fs.writeFile(tmpFileName, content, function (err) {
if (err) throw err;
console.log('Temp file creation successful.');
});
})
}
function deleteTmpFile(spec){
return new Promise(resolve => {
fs.unlink(tmpFileName, function (err) {
if (err) throw err;
console.log('Temp file deletion successful.');
});
})
}
However, in my console output, I only get
Temp file creation successful.
Temp file deletion successful.
However, if I delete await deleteTempFile(), then Hurray, finished! shows up on the console.
And more generally, how do I debug these patterns of problems?
Why is this happening?
I have rewritten your code, to showcase how to use promises.
Promise callback gets two functions as arguments: resolve and reject.
You should call resolve when operation finishes with success, and reject when it fails.
// I moved `tmpFileName` variable from here into the request handler,
// because it was "global" and would be shared between requests.
app.get('/clicked', async (req, res) => {
let tmpFileName = "tmp" + Math.random().toString() + "tsl"
let writingResult = await writeTmpFile(tmpFileName, "hello, world!")
let deletionResult = await deleteTmpFile(tmpFileName)
res.send({ writingResult, deletionResult })
console.log("Hurray, finished!")
})
function writeTmpFile (filename, content) {
return new Promise((resolve, reject) => {
fs.writeFile(filename, content, function (err) {
// on error reject promise with value of your choice
if (err) reject(err)
// on success resolve promise with value of your choice
resolve('Temp file creation successful.')
})
})
}
function deleteTmpFile (filename) {
return new Promise((resolve, reject) => {
fs.unlink(filename, function (err) {
if (err) reject(err)
resolve('Temp file deletion successful.')
})
})
}
For working with the file you can use writeFileSync instead writeFile. (Reference).
For multiple Promise you can use the Promise.all method.
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo');
});
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
});
from MDN
I would like to get my data out from the sqlite db.each function using the promise object and the async/await I tried to do it but I don't really understand how to do it and I would like some explanations
my code below :
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./data.sqlite', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the database');
});
function request (req) {
new Promise(function (resolve, reject) {
db.each(req, function (err, row) {
if (err)
reject(err);
else
data.push(row);
}, function (err, n) {
if (err) {
reject(err);
}
else
resolve(data);
});
});
}
data = await request("select * from user "); //I'm getting errors here
db.close((err) => {
if (err) {
return console.error(err.message);
}
console.log('Close the database connection');
});
await can only be used inside an async function. Create an async function, inside this function, await the promise returned by request function
async function makeRequest() {
data = await request("select * from user ");
}
and you need to return the promise from request function so that it can be awaited
function request (req) {
return new Promise(function (resolve, reject) {
// code
});
}
try {
const promise = new Promise(async (resolve, reject) => {
reject('oe')
})
.catch(async (err) => {
console.log('bbbbb', err)
throw err
})
} catch (err) {
console.log('aaaaa', err)
}
Is is possible to make aaaaa loggable
In general, it doesn't make sense to pass an async function into a promise's then or catch function. And it never makes sense to pass one into the promise constructor. If you're going to go async, do it earlier. Also, when you want a rejected promise for testing, etc., just use Promise.reject('oe').
In order to catch an error from an async function with a try/catch, you must be in an async function. In that case, the minimal change to your example would be to await the result of the call to catch:
// Assuming this is in an `async` function, making only minimal changes
try {
const promise = new Promise(async (resolve, reject) => {
reject('oe')
})
.catch(async (err) => {
console.log('bbbbb', err)
throw err
})
await promise; // ***
} catch (err) {
console.log('aaaaa', err)
}
If you aren't in an async function, you can't use try/catch to catch errors from promises (which includes from an async function call, since they return promises). Instead, you have to use the promise returned by catch:
// Assuming this is NOT in an `async` function, making only minimal changes
const promise = new Promise(async (resolve, reject) => {
reject('oe')
})
.catch(async (err) => {
console.log('bbbbb', err)
throw err
})
.catch(err => {
console.log('aaaaa', err)
})
Making larger changes, if you're already in an async function, get rid of the then and catch calls:
// Assuming this is in an `async` function
try {
try {
await Promise.reject('oe');
} catch (innerError) {
console.log('bbbbb', innerError);
throw innerError;
}
} catch (outerError) {
console.log('aaaaa', outerError);
}
You can use async/await:
async function test() {
try {
const promise = await new Promise(async (resolve, reject) => {
reject('oe')
}).catch(async err => {
console.log('bbbbb', err)
throw err
})
} catch (err) {
console.log('aaaaa', err)
}
}
test()
I am trying to run this function but the it keeps returning undefined when I explicitly hardcode the return value.
const splitVideo = async (sid, video, part) => {
let framesLocation =`${process.cwd()}/${dirs.videoFrames}/${sid}_${part}`;
console.log(fs.existsSync(framesLocation));
if(!fs.existsSync(framesLocation)) {
console.log("making dir");
f.s.mkdirSync(framesLocation);
}
ffmpeg(video)
.on('end', () => {
return "done";
})
.on('error', (err) => {
throw err;
})
.screenshots({
timestamps: [1,2],
filename: `${sid}_${part}/frame-%s.png`,
folder: `${process.cwd()}/${dirs.videoFrames}`
});
};
Please help this is very frustrating.
Your function does not return anything, thats why you are getting undefined. Wrap the ffmpeg call in new Promise(...) to be able to resolve its asynchronous result:
const splitVideo = async (sid, video, part) => {
// ...
return new Promise((resolve, reject) => {
ffmpeg(video)
.on('end', () => {
resolve("done");
})
.on('error', (err) => {
reject(err);
})
.screenshots({
timestamps: [1,2],
filename: `${sid}_${part}/frame-%s.png`,
folder: `${process.cwd()}/${dirs.videoFrames}`
});
};
};
const ret = await splitVideo(...);
console.log(ret);
Also note you need to await this function to be able to read the result (or get the result in then handler).
You can only await promises.
const splitVideo = async (sid, video, part) => {
// ...
const val = await new Promise((resolve, reject) => {
ffmpeg(video)
.on('end', () => {
resolve("done");
})
.on('error', (err) => {
reject(err);
})
.screenshots({
timestamps: [1,2],
filename: `${sid}_${part}/frame-%s.png`,
folder: `${process.cwd()}/${dirs.videoFrames}`
});
//... more code using the val maybe?
return val
};
};
The problem is that your ffmpeg(video) function inside your splitVideo async function is asynchronous. What's happening is that your splitVideo function is being called but it's returning undefined before your ffmpeg(video) function accomplishs. What can you do to resolve this?
You already defined your splitVideo function as an async function, this allows you to use the reserved word "await". But first let's encapsulate your ffmpeg(video) function inside a promise.
const splitVideo = async (sid, video, part) => {
//Your code
let promise = new Promise((resolve, reject) => {
ffmpeg(video)
.on('end', () => {
resolve("done");
})
.on('error', (err) => {
reject(err);
})
.screenshots({
timestamps: [1,2],
filename: `${sid}_${part}/frame-%s.png`,
folder: `${process.cwd()}/${dirs.videoFrames}`
});
});
try{
return await promise;
}catch(err){
return err;
}
This should be enough for your splitVideo function, but it's importante to pay attention cause unhandled promises rejections are deprecated and in the future will terminate your node.js process. What you need to do is also add a .catch() statement to you splitVideoFunction, something like:
splitVideo.catch((err) => {//your code}).then((result) => {//Your code})
Or your can call splitVideo inside an another async function and use:
try {
await splitVideo(video);
}catch(err){
//Your code
}
If you have any doubt about async/await like I had, you may find this question useful.
Node.JS - Can`t get async throws with try/catch blocks
I hope it helps.
I need to get a value of an asynchronous function. I tried to use Promise, but that does not work:
const res = new Promise(function (resolve, reject) {
gm(readStream).size({ bufferStream: true }, function (err, size) {
if (!err) resolve(size)
})
})
console.log(res)
The result I get is Promise { <pending> }
Promises are an abstraction for callbacks, not magic. They can't make asynchronous code synchronous.
The correct solution is:
const res = new Promise(function (resolve, reject) {
gm(readStream).size({ bufferStream: true }, function (err, size) {
if (err) reject(err);
else resolve(size);
})
});
res.then(function(promiseResolutionValue) {
console.log(res)
})
You could also use async / await here:
const getSize = readStream => {
return new Promise(function (resolve, reject) {
gm(readStream).size({ bufferStream: true }, function (err, size) {
if (err) reject(err);
else resolve(size);
})
});
}
let printSize = async readStream => {
console.log(`Size is ${await getSize(readStream)}`);
}
Or, if you're using NodeJS (Version 8+), you might be able to adapt your function to use util.promisify.
Other Promise libraries, such as Bluebird, also offer such functions, to easily convert 'standard' node-style functions (functions that have a callback with err, data as arguments) into promise-returning equivalents.
Or just use the callback.
Your code should be looked like this:
const res = new Promise(function (resolve, reject) {
gm(readStream).size({ bufferStream: true }, function (err, size) {
if (!err) resolve(size)
else reject(err)
})
})
function onResolved(data) {
console.log('data: ', data);
}
function onRejected(err) {
console.log('err:', err);
}
res.then(onResolved, onRejected);
Promise does not make your code synchronous, it lets you control when you want to get the result, not immediately like callback style.