3

My current code is

resizer.resize(filepath, parsedUrl, fullDestinationPath, function() {
        return self.send(response, 200, {'Content-Type': mime.lookup(fullDestinationPath)}, fs.createReadStream(fullDestinationPath));
    });

The resize runs to

Resizer.prototype.resize = function (filepath, parsedUrl, fullDestinationPath) {
         this.read(filepath, parsedUrl, fullDestinationPath);
};

Which then calls

Resizer.prototype.read = function(filepath, parsedUrl, fullDestinationPath){ 
    something.then(function (somethingElse) {
      // Not relevant for question
   }).catch(function (err) {
       console.error(err);
   });
};

I've had a console.log(1); after this.read in the second code snippet to guarantee that it was completely running through it. But returning to my first snippet it doesn't call my callback. The reason I'm using a callback there is because if I don't the send will be executed before a file is completely saved unless it's a very small file, so self.send needs to be called AFTER .resize which is why I am attempting to use a callback.

I've tried multiple things thinking a syntax error might have be the issue or that it was hanging on something, but I've verified it isn't and it is simply not calling the callback. Did I make some kind of obvious mistake? How do I make it call the callback?

I have read questions/answers like this one How to make a function wait until a callback has been called using node.js and understand how it works and implemented it in the same manner, but it's not working for me and I do not see why. Thanks for taking the time to read through this.

EDIT: After fixing the issue with Roberts answer I've removed the callback and changed the code to this, I am using promises instead of callbacks tho: Snippet 1:

resizer
        .resize(filepath, parsedUrl, fullDestinationPath)
        .then(function() {
            return self.send(response, 200, {'Content-Type': mime.lookup(fullDestinationPath)}, fs.createReadStream(fullDestinationPath));
        });

Snippet 2:

Resizer.prototype.resize = function (filepath, parsedUrl, fullDestinationPath) {
   return this.read(filepath, parsedUrl, fullDestinationPath);
};

Snippet 3:

Resizer.prototype.read = function(filepath, parsedUrl, fullDestinationPath){
    return Jimp.read(filepath)
        .then(function() {
            return //tons of irrelevant code
         })
        .catch(function (err) {
            console.error(err);
        });
};
Community
  • 1
  • 1
Strah Behry
  • 551
  • 2
  • 8
  • 25
  • I don't see any callback being passed to or used by your resize function. – Robert Moskal May 10 '16 at 11:54
  • The return self.send part is the part that isn't being called. Don't you call that a callback? Also using console.log I have verified that it is running through all code and not hanging anywhere, it is just not getting to the return. The self is just a var self = this; in a upper part of the code since the meaning of this changes when I put another function around it. – Strah Behry May 10 '16 at 11:56
  • `self.send()` is a command inside an anonymous function definition which you pass as an argument to `resize`, but as mentioned here, `resize` does not accept a callback in its parameters list (it only accepts filepath, parsedUrl and fullDestinationPath) – OzW May 10 '16 at 11:59

1 Answers1

4

Resizer.prototype.resize doesn't take a callback argument to begin with, so even though you pass one from your main code, there's nothing done with it.

The same goes for this.read(): it has no callback argument either.

Try this:

Resizer.prototype.resize = function (filepath, parsedUrl, fullDestinationPath, callback) {
  this.read(filepath, parsedUrl, fullDestinationPath, callback);
};

And:

Resizer.prototype.read = function(filepath, parsedUrl, fullDestinationPath, callback){ 
  something.then(function (somethingElse) {
    callback(null, somethingElse); 
  }).catch(function (err) {
    console.error(err);
    callback(err);
  });
};
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • Thank you it is working now, I didn't realize I had to pass the callback as a parameter, I've had to edit your second snippet a tiny bit to make it work, I'll edit the code I used in my main post. – Strah Behry May 10 '16 at 12:02
  • There's nothing really special about callbacks, they are just functions that get passed around and called once an operation has completed :D – robertklep May 10 '16 at 12:04
  • Thanks I won't forget how to properly use callbacks for sure now. – Strah Behry May 10 '16 at 12:06
  • I added the code I ended up with in a edit, is that a proper way to solve it? – Strah Behry May 10 '16 at 12:23
  • @StrahBehry yeah that looks okay, although you're now using promises instead of callbacks. Which is totally fine and, in fact, will allow you to drop the `callback` arguments completely (promises are a different way of handling asynchronous code, an alternative to callbacks). – robertklep May 10 '16 at 12:35