gomfunkel / node-exif

A node.js library to extract Exif metadata from images.
MIT License
581 stars 104 forks source link

Return results #63

Open gorbypark opened 7 years ago

gorbypark commented 7 years ago

Is it possible to return the results to a parent function? I can't seem to get exifData to return. Even if i throw something like return('test test'); as the first line after ExifImage({.... it doesn't return.

var ExifImage = require('exif').ExifImage;

function myExif() {
    try {
        new ExifImage({image: 'exif4m.jpg'}, function (error, exifData) {
            if (error) {
                console.log('Error: ' + error.message);
                return null;
            } else {
                return exifData; // Do something with your data!
            }
        });
    } catch (error) {
        console.log('Error: ' + error.message);
        return null;
    }
}

console.log(myExif());

The console.log returns undefined.

ghost commented 7 years ago

You can solve this with a promise:

getEXIF( filePath ) {
        return new Promise(resolve => {
            ExifImage(filePath, (err, data) => {
                resolve(data);
            });
        });
    }

I call the getEXIF method on async function with await:

async function whatever() {
  // snip
  const response = await getEXIF('path to image');
  // use response here
  // snip
}

But you can of course resolve the promise traditional way. It's on you.