1

Node.JS returns the callback

 2 years ago
source link: https://www.codesd.com/item/node-js-returns-the-callback.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Node.JS returns the callback

advertisements

I am using this plugin manager https://github.com/c9/architect and creating a node module. The problem I am having is that I want to expose an api from my node module to the host application. The problem is that the plugin manager uses callbacks to signal that all plugins have been registered.

Example: In my main app, I require my api module that I am creating

var api = require('apiModule')

And in my node_modules directory

module.exports = (function apiModule(){

    architect.createApp(config, function(err, app){
        if(err) throw err;

        return app

    });

})();

This obviously does not work, but shows that I am trying to return the value of app to the main application.

How can I get the value of app back to the api variable?


You could pass a callback to your your module :

module.exports = function(callback){

    architect.createApp(config, function(err, app){
        if(err) throw err;

        return callback(app); //you should check if callback is a function to prevent error

    });

});

var api = require('apiModule');
api(function(app) {
    console.log(app); //you access your app

})




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK