Jones
Jones

Reputation: 33

nodejs wait for a function to execute

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

Upvotes: 0

Views: 1139

Answers (3)

XCEPTION
XCEPTION

Reputation: 1753

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();

Upvotes: -1

Jaffer
Jaffer

Reputation: 2968

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);
});

Upvotes: 1

Bálint
Bálint

Reputation: 4039

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); 
});

Upvotes: 1

Related Questions