PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

[FIXED] What does mean a variable on the then block without a value assingment?

 November 04, 2022     lambda, node.js, promise, variables     No comments   

Issue

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.


Solution

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.



Answered By - junvar
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing