Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a callback that requires info of its child function

I am working in a project that uses Node.js for a Haraka (an smtp server) plugin.

This is Node.JS and I have a little problem whith callbacks. I haven't been able to convert this particular code to use a callback.

So, this is the code that I have:

exports.hook_data = function (next, connection) {
    connection.transaction.add_body_filter('', function (content_type, encoding, body_buffer) {
        var header = connection.transaction.header.get("header");
        if (header == null || header == undefined || header == '') return body_buffer;

        var url = 'https://server.com/api?header=' + header ;
        var request = require('request');
        request.get({ uri: url },
          function (err, resp, body) {
              var resultFromServer = JSON.parse(body);
              return ChangeBuffer(content_type, encoding, body_buffer, resultFromServer);
          }
        );
    });
    return next();
}

This code does not work because It doesn't wait the callback of the Request to continue. I need to finish the request before next();

And these are the requirements:

  1. At the end of exports.hook_data is mandatory to return next(). But it only have to return it after the request.
  2. I need to return a Buffer in add_body_filter but I need to create the buffer with information getted from a server.
  3. To make the Get Request I need to use a parameter (header) that I only have inside add_body_filter.

So the issue is that I can not make the request before add_body_filter and put the result inside a callback because the parameter that I need to make the request are only inside add_body_filter.

Any advice please?

like image 900
Ricardo Polo Jaramillo Avatar asked Dec 15 '15 20:12

Ricardo Polo Jaramillo


People also ask

How do you create a callback function?

In JavaScript, the way to create a callback function is to pass it as a parameter to another function, and then to call it back right after something has happened or some task is completed.

How do you call a function inside a callback?

You need to use the . call() or . apply() methods on the callback to specify the context which the method is called upon. The callback method remote_submit does not know what this will be anymore and thus when it calls the callback methods they're executed like normal functions not on an object.

How do you make a callback asynchronous?

Callbacks are not asynchronous by nature, but can be used for asynchronous purposes. In this code, you define a function fn , define a function higherOrderFunction that takes a function callback as an argument, and pass fn as a callback to higherOrderFunction .

Why callback function is required?

Need of Callback Functions. We need callback functions because many JavaScript actions are asynchronous, which means they don't really stop the program (or a function) from running until they're completed, as you're probably used to. Instead, it will execute in the background while the rest of the code runs.


1 Answers

Unless you are willing to use synchronous, blocking functions, it is impossible to satisfy the requirements you have numbered.

I would examine reasons underlying each requirement and see if you accomplish your goals in a different way.

On face value, looking at just the code you have there, I would look for a way to alter the contract of exports.hook_data so that you're able to call next() from inside the request.get callback.

like image 138
Hurricane Hamilton Avatar answered Nov 13 '22 22:11

Hurricane Hamilton