hyprstack
hyprstack

Reputation: 4228

`async.series` not reaching second function

I have a sequence of modules I need to run in node, using async. I have async.waterfall nested within async.series, but am having trouble reaching the second function in the async.series method. The process ends after printing "Processing images completed". This is running in aws lambda.

What am I doing wrong?

These is my module:

S3resizer.rs

    S3resizer.rs = function (imgName, bucketName, sizesObj, imageType, obj, cb) {

        var dir = "/tmp";

        async.series([
            function (next) {

                async.waterfall([
                    function (callback) {
                        S3get(bucketName, imgName, callback);
                    },
                    function (data, callback) {
                        async.each(sizesObj, function (sizesObj, mapNext) {
                            rs(data, imgName, dir, sizesObj, mapNext);
                        }, function (err) {
                            if (err) {
                                console.log("Error when resizing images, %s", err);
                                callback(err);
                            } else {
                                console.log("Processing images completed");
                                callback();
                            }
                        });
                    }
                ], function (err) {
                    console.log("Fetched and resized ok!");
                    next(null);
                });
            },

            function (next) {
                async.waterfall([
                    function (callback) {
                        console.log("reached readfile");
                        readDirFile(dir, callback);
                    }
                ], function (error, results) {
                    if(error) {
                        next(error);
                    } else {
                        next(null, results);
                    }
                });
            }
        ], function (err, results) {
            if (err) {
                cb(err);
            } else {
                cb(null, results);
            }
        });
    };

The 'rs' module:

'use strict';

// dependencies
var gm = require('gm').subClass({ imageMagick: true });

var resizer = {};

resizer.resize = function (data, imgName, directory, sizesObj, callback) {

    if (data.hasOwnProperty('Body')) {
        data = data.Body;
    } else {
        data = data;
    }

    gm(data)
        .resize(sizesObj.width, sizesObj.height)
        .write(directory + "/" + sizesObj.name + "_" + imgName, function (err) {
            if (err) {
                console.error("Error resizing image, %s", err.message);
                callback(err);
                return;
            }
            console.log("Wrote to %s directory, with size name %s and image name %s", directory, sizesObj.name, imgName);
            callback(null);
        });

};

module.exports = resizer;

Upvotes: 0

Views: 219

Answers (1)

robertklep
robertklep

Reputation: 203231

This doesn't look right:

if (err) {
    console.log("Error when resizing images, %s", err);
} else {
    console.log("Processing images completed");
    callback;
}

First, you're not calling callback when an error occurs. And second, you're not actually calling the callback function as it's missing parentheses (i.e. callback()).

Upvotes: 1

Related Questions