How to get data from a query in a function from another js File


When i try to show data in console,just show can i make a async or callback with this js

Main JS ->

const { app, BrowserWindow , ipcMain} = require('electron');
var tools = require('./app/connection.js');

app.on('ready',() => {
console.log('App iniciado.');
let mainWindow = new BrowserWindow({


ipcMain.on('pega-numero-usuarios',() => {
console.log('Numero de usuarios');
var obj = tools.getUsuarios();

Connection.js ->

 const sql = require('mssql')

const config = {
user: 'sa',
password: '.',
server: ';;', 
database: 'VMD',
parseJSON: true

module.exports = {
getUsuarios: function () {
sql.connect(config, function (err) {
  new sql.Request().query('select count(cod_usuari) as contador from USUAR', function (err, result) {
    //console.dir("Quantity: " + result[0].contador)
    return result;



The mssql documentation includes an example for a similar case. What you want to do is just split that over two files.

What do you see when you change the second-to-last line in the main file to console.log(obj)?


Returns undefined, but if i put a console.log(obj) in the Connection, it returns the value


As it happens, I have a project ongoing where I’m doing something very similar. I have an external module where I export a function that returns a Promise. In my Atom package, I import that module and then run the .then() method of the returned Promise in order to tell my view to update its items.

This is the easiest way I’ve found to use async methods. Promises are weird and they don’t feel like they fit in with anything else, but the thing is that they work best when they aren’t expected to fit in. Once you create a Promise, let it handle all the details of its own resolution.