What is different from Consign’s "include" and "then" methods?

Asked

Viewed 39 times

-1

I’m using the Consign library in a back-end creation exercise on Node.js.

Part of the code appears like this:

consign()
    .include('./config/passport.js')
    .then('./config/middlewares.js')
    .then('./api')
    .then('./config/routes.js')
    .into(app)

I understand that the .then loads the JS code from the respective files. But and .include? What’s the difference between them?

  • 1

    I’m not familiar with this library. Have you tried looking at the documentation?

  • 2

    Just look at the source code. You will notice that the then is nothing more than calling the last executed function again only with another meter. Despite the name, the function does not seem to work with Promises.

  • 1

    Here the code: Consign.prototype.then = function(entity) {
 this[this._lastOperation].call(this, entity);
 return this;
};

  • So basically in this code I posted, where I’m using just include, use . then and . include is the same thing??? Why did they create that . then???

1 answer

1


Although the library uses a function called then, do not confuse with Promises.

The then, in this context, executes the last executed function. See source code:

Consign.prototype.then = function(entity) {
  this[this._lastOperation].call(this, entity);
  return this;
};

In your example, you call include first and then a call trail then. In that case, you’re, behind the scenes, calling include.

The then makes sense when you use functions other than include. Take this example:

consign()
    .include('./config/passport.js') // muda o _lastOperation para include
    .then('./config/middlewares.js') // chama o include
    .then('./api')                   // chama o include
    .then('./config/routes.js')      // chama o include
    .exclude('./config/test.js')     // muda o _lastOperation para exclude
    .then('./config/test.js')        // chama o exclude
    .then('./config/test2.js')       // chama o exclude
    .into(app)

At the end of the day, it’s just a syntactic sugar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.