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.