Reputation: 45320
How can I use .then()
chaining to call a function that is declared in Node.js? The problem is after the call to getFileType()
I want to call getExif()
but this is throwing a syntax error:
'use strict';
const FileType = require('file-type');
const exif = require('jpeg-exif');
const path = './alcatraz.jpg';
async function getFileType(filePath) {
return await FileType.fromFile(filePath);
}
function getExif(filePath) {
return new Promise((resolve, reject) => {
exif.parse(filePath, (error, result) => {
if(error) {
reject(error);
} else {
resolve(result);
}
});
});
}
getFileType(path).then((result) => {
console.log(result);
})
# ======> problem is here <======
.then(getExif(path) {
console.log(result);
})
.catch((error) => {
console.error(error.message);
process.exit(3);
});
Syntax error is:
SyntaxError: missing ) after argument list
Upvotes: 3
Views: 99
Reputation: 36
Maybe you want to do this to get result of getExif.
getFileType(path)
.then((result) => {
console.log(result);
})
.then(getExif(path)
.then(resultOfGetExif => console.log(resultOfGetExif))
)
.catch((error) => {
console.error(error.message);
process.exit(3);
});
or this just running getExif
getFileType(path)
.then((result) => {
console.log(result);
return result;
})
.then((result) => {
getExif(path)
console.log(result);
})
.catch((error) => {
console.error(error.message);
process.exit(3);
});
I hope these are helpful.
Upvotes: 0
Reputation: 370629
You should return the call to getExif(path)
inside the upper .then
, so that the lower .then
can consume it:
getFileType(path).then((result) => {
console.log(result);
return getExif(path);
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error(error.message);
process.exit(3);
});
The result
variable in the lower .then
will contain the result of calling getExif
. If you want both the getFileType
and getExif
resolve values, since they don't look to depend on each other, use Promise.all
instead:
Promise.all([
getFileType(path),
getExif(path),
])
.then(([fileType, exif]) => {
// work with results
})
.catch((error) => {
console.error(error.message);
process.exit(3);
});
Upvotes: 3