Home > Blockchain >  What does mean a variable on the then block without a value assingment?
What does mean a variable on the then block without a value assingment?

Time:07-07

I'm working on this code in node, on this class

'use strict';
const loader = require('./loader');
const path = require('path');
const config = require('../config/config');

module.exports.loadModels = function (confDataBase) {
  return loader.loadModels({
    models: path.join(process.cwd(), 'models'),
    hooks: '',
    methods: path.join(process.cwd(), 'db', 'methods')
  }, confDataBase);
}

module.exports.loadModelsDefault = function () {
  delete global.db;
  return exports.loadModels(config[process.env.NODE_ENV || 'development'])
    .then(() => global.db)
}

The last fraction on loadModelsDefault uses a .then function and it just passes the global.db variable to a lambda function and doesn't do anything. I'm supposing it assigns the output of the exports.loadModels to the variable global.db, as it was deleted before, but i need confirmation, as i don't know that kind of syntaxis.

CodePudding user response:

It does nothing.

Presumably, the intent was to initialize the key db in the object global. The delete would uninitialize it, then the .then() would re-initialize it, so to global.db would indicate whether the db was ready to be used.

But that won't occur:

let global = {}
let initDb = () => global.db;

initDb();
console.log(global); // `{}`, NOT `{db: undefined}`

global is a special Node object. So it's possible it behaves differently than normal objects if e.g. it has getters/setters that override the default behavior. But that doesn't seem to be the case with a quick check.

  • Related