1

I have this node.js function process() which is supposed to return a value when called. I am facing issue with creating a callback for my process(). The value should return from process() only after it gets response back from ec2.runInstances(params, function(err, data) call.

------------- Snippet from app.js (express.js)--------------------

var createEngine = require('./ec2_create.js');
app.get('/create', function (req, res) {
    res.render('create', {
        status: createEngine.process()
    });

});

------------------------Code snippet from ec2_create.js -----------------------

function process(callback) {

    var status = null;
    // Create the instance
    ec2.runInstances(params, function (err, data) {
        if (err) {
            //console.log("Could not create instance", err); 
            status = "Could not create instance: " + err;
        } else {
            var instanceId = data.Instances[0].InstanceId;
            //console.log("Created instance", instanceId);
            status = "Created instance: " + instanceId;
        }

    });
    callback(status);
};

module.exports.process = process;
1
  • 1
    You should avoid using the name 'process' for your function. There is already a global object in Node called 'process' nodejs.org/api/globals.html#globals_process. Commented Oct 31, 2013 at 7:27

3 Answers 3

1

Try as follows

function ec2process(callback){

var status = null;
// Create the instance
ec2.runInstances(params, function(err, data) {
if (err) { 
    //console.log("Could not create instance", err); 
    status = "Could not create instance: " + err;   
    }
else{
    var instanceId = data.Instances[0].InstanceId;
    //console.log("Created instance", instanceId);
    status = "Created instance: " + instanceId; 
  } 
callback(status); // Callback moved

});


};

exports. process = ec2process

Sign up to request clarification or add additional context in comments.

Comments

1

since your process method expects a callback function and does not return a value, you could call it rather like this:

app.get('/create', function (req, res) {
    createEngine.process(function(status){
        //you're inside the callback and therefor have the status value
        res.render('create', {
            status: status
        });
    }):  
});

Comments

0

You should move the code which calls the callback into the callback for runInstances:

function process(callback) {

  var status = null;
  // Create the instance
  ec2.runInstances(params, function (err, data) {
    if (err) {
        //console.log("Could not create instance", err); 
        status = "Could not create instance: " + err;
    } else {
        var instanceId = data.Instances[0].InstanceId;
        //console.log("Created instance", instanceId);
        status = "Created instance: " + instanceId;
    }
    callback(status);
  });

};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.