javascript - understanding module.exports with callback - Stack Overflow

I have a situation where I am creating a node module that returns only when an asynchronous operation i

I have a situation where I am creating a node module that returns only when an asynchronous operation is pleted. One way to do this (shown below), is to assign module.exports a function with a callback parameter. Inside the function, you would then return the callback.

Here's an example of what I describe, with done being callback:

// module called test.js
module.exports = function(done) {
  // do something asynchronous here
  process.nextTick(function() {
    done();  //  call done when the asynchronous thing is plete...
  }
}

Where I am getting hung up, is in how the callback done is indeed being executed, considering I don't define it anywhere...

For example, in vanilla javascript, I can pass done as parameter and then call it within the function, as long as I create the callback function in the invocation.

function testAsyncCb(msg, done) {
  console.log(msg);
  setTimeout( function() {
    done();
  }, 1000);
  console.log("last line in code");
}

testAsyncCb("testing", function(){ console.log("done"); });  // invocation with callback function

Back in the first node example, somewhere the call to module.exports by require() is creating a function for the done() to resolve to right? If not, how is the callback resolving?

Having a hard time finding information for how this works. Any help/direction is appreciated.

I have a situation where I am creating a node module that returns only when an asynchronous operation is pleted. One way to do this (shown below), is to assign module.exports a function with a callback parameter. Inside the function, you would then return the callback.

Here's an example of what I describe, with done being callback:

// module called test.js
module.exports = function(done) {
  // do something asynchronous here
  process.nextTick(function() {
    done();  //  call done when the asynchronous thing is plete...
  }
}

Where I am getting hung up, is in how the callback done is indeed being executed, considering I don't define it anywhere...

For example, in vanilla javascript, I can pass done as parameter and then call it within the function, as long as I create the callback function in the invocation.

function testAsyncCb(msg, done) {
  console.log(msg);
  setTimeout( function() {
    done();
  }, 1000);
  console.log("last line in code");
}

testAsyncCb("testing", function(){ console.log("done"); });  // invocation with callback function

Back in the first node example, somewhere the call to module.exports by require() is creating a function for the done() to resolve to right? If not, how is the callback resolving?

Having a hard time finding information for how this works. Any help/direction is appreciated.

Share Improve this question edited Oct 15, 2015 at 19:24 shai asked Oct 15, 2015 at 16:45 shaishai 551 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Think of module.exports as an object (module.exports = {}). So whatever you put to the object will be publicly visible to anyone do require module.

For instance, you have

 module.exports = function myFunc() {} 

then require to that would mean

var abc = require('./my-module'); --> abc == myFunc

if you would do

module.export.myFunc = function () {}

than require would be

var abc = require('./my-module'); --> abc == {myFunc: function () {}}

require operation is sync, not async like in requirejs (meaning not AMD but more like monjs).

see http://www.sitepoint./understanding-module-exports-exports-node-js/ for more info also for nodejs official docs: https://nodejs/api/modules.html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742362787a4429716.html

相关推荐

  • javascript - understanding module.exports with callback - Stack Overflow

    I have a situation where I am creating a node module that returns only when an asynchronous operation i

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信