0

How can I wait for the function to return.Is there a way to wait for a function to finish executing before I continue with my code. I would like to wait for the createThumbnail function to return the buffer before I continue. Thank you.

 createThumbnail: function(imagepath){
    Jimp.read(imagepath).then(function (lenna) {
        lenna.resize(256, 256)            // resize
            .quality(60)                 // set JPEG quality
            //  .greyscale()                 // why on earth would i need black and white
            .getBuffer(Jimp.MIME_JPEG,function(err, buffer, callback){ // I have other Options like png etc.

                return buffer;

            })
    }).catch(function (err) {
        console.error(err);
    });

},

and then in another file i call this function

var thum_image = functions_api.createThumbnail(Imagepath);
console.log(thum_image); // its null
1
  • 2
    Read about callback and Promise in JavaScript.. Commented Aug 16, 2016 at 10:23

3 Answers 3

1

You can use callbacks.

Callbacks are functions which get executed once something is done, in your case the image is finally loaded.

Instead of taking only 1 parameter, take an additional function you call after you loaded the image:

createThumbnail: function(imagepath, callback){
    Jimp.read(imagepath).then(function (lenna) {
        lenna.resize(256, 256)            // resize
            .quality(60)                 // set JPEG quality
            //  .greyscale()                 // why on earth would i need black and white
            .getBuffer(Jimp.MIME_JPEG,function(err, buffer, callback){ // I have other Options like png etc.
                callback(buffer);
                return buffer;

            })
    }).catch(function (err) {
        console.error(err);
    });

},

Then you can call the function like

var thum_image = functions_api.createThumbnail(Imagepath, function (image) {
   console.log("Loaded!", image); 
});
Sign up to request clarification or add additional context in comments.

4 Comments

I get an error. callback is not a function when I try your solution.
@Jones Sounds like you haven't changed the way you are calling the function, you need to add the callback in as the second argument.
If I remove the second callback parameter it works but its still gives me null.
It will always give you null, but the callback function can tell you when the image is finally loaded
1

You are already using promise with Jimp package. based on that you can use promise in your code as well

createThumbnail: function(imagepath) {
    return new Promise(function (resolve, reject) {
       Jimp.read(imagepath).then(function (lenna) {
            lenna.resize(256,256)
            .quality(60)
            .getBuffer(Jimp.MIME_JPEG,function(err, buffer, callback) {
                if(!err)
                    resolve(buffer);
                else
                    reject(err);
            })
        }).catch(function (err) {
            console.error(err);
            reject(err);
        });
    });
}


var thum_image = functions_api.createThumbnail(Imagepath)
.then(function (thum_image) {
    console.log(thum_image);
})
.catch(function (err) {
    console.error(err);
});

Comments

-1

You need to use Promises. Check out the packages q, async

In q you can do the following:

var q = require('q')
createThumbnail: function loadImage(imagepath) {
    var deferred = q.defer();
    Jimp.read(imagepath).then(function (lenna) {
        lenna.resize(256, 256)            // resize
            .quality(60)                 // set JPEG quality
            //  .greyscale()                 // why on earth would i need black and white
            .getBuffer(Jimp.MIME_JPEG,function(err, buffer, callback){ // I have other Options like png etc.
                deferred.resolve(buffer)
            })
    }).catch(function (err) {
        deferred.reject(err)
    });
}

//WHEREVER YOU WANT THE BUFFER
createThumbnail(imagepath).then(function(buffer) {
    console.log(buffer)
}).catch(function(error) {
    console.log(error)
}).done();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.