0

Before asking this question, yes I have read the incumbent question here and it still makes no sense to me hence marking this a duplicate is not helpful when the other question does not give me an answer I can understand.

  1. I want a function to return a value (JSON data from a database).
  2. Whenever that function is called from another file, I want the value to be returned to the calling page.
  3. That's it.

So here is my function in my .js file:

async function rsOffersAll() {
    await global.MYDB.connect(); // global variable already setup using mssql module in app.js
    try {
        global.MYDB.request(global.MYDB).query('SELECT Top(10) * FROM [Broadcast].[Offer]').then(function(result) {
            console.dir(result); // shows correct data
            return JSON.stringify(result); // how do I return this value so that any other file calling rsOffersAll gets THIS result?
        });
    } catch (error) {
    }
}

module.exports.rsOffersAll = rsOffersAll;

In my calling .js file I have this:

const Offer = require('../models/broadcasts/offer');

(async function rsOffersAll() {
      let rsOffersAll = await Offer.rsOffersAll();
      console.log(rsOffersAll); // this should log the JSON data in the console but I get "undefined" instead
    })();

How can I edit the above code so that the data is returned to the calling page? I know that for some reason nothing is being returned, but I don't know why (linking to another question that asks a similar question is not going to help, trust me). Could someone be kind enough to explain it using my code above?

volume one
  • 6,800
  • 13
  • 67
  • 146
  • 2
    I don't think this is related to your issue, but you should avoid giving variables the same name as the function that contains them (as in your second snippet). In this case, I don't think the enclosing function needs to have a name. – JLRishe Nov 17 '19 at 13:53
  • 3
    To me it helps make `async/await` coding easier if you consciously try as hard as possible to completely avoid `.then()` callbacks. Really, avoiding that is the point of `async/await`. – Pointy Nov 17 '19 at 14:01
  • @Pointy do you mean don't use `.then()` and instead put an `await` in front of the `global.MYDB.request(global.MYDB).query()` part? – volume one Nov 17 '19 at 16:42
  • Right. You can `await` the `.request()` database call, and the stuff that's in your `.then()` callback would go after that. – Pointy Nov 17 '19 at 16:56

2 Answers2

2

You aren't returning anything from rsOffersAll. The then() callback is returning something, but that's it. Try this, to fix it:

async function rsOffersAll() {
    await global.MYDB.connect(); // global variable already setup using mssql module in app.js
    try {
        return global.MYDB.request(global.MYDB).query('SELECT Top(10) * FROM [Broadcast].[Offer]').then(function (result) {
            console.dir(result); // shows correct data
            return JSON.stringify(result); // how do I return this value so that any other file calling rsOffersAll gets THIS result?
        });
    } catch (error) {}
}
Wang Liang
  • 4,244
  • 6
  • 22
  • 45
stewartmcgown
  • 516
  • 4
  • 16
  • 1
    Thank you! This has been killing me for the past week to understand it. The only comments I keep getting is that I'm not returning anything but I just didn't understand where it was supposed to return from – volume one Nov 17 '19 at 16:41
  • No problem. Remember that `.then()` always returns a promise and `async function` just wraps the result in a promise if it isn't already. ie returning something from inside of a callback (`.then(a => a.length)`) evaluates to a promise. – stewartmcgown Nov 18 '19 at 15:53
1

Let's rewrite it to make it more obvious.


async function extractResult(result) {
            console.dir(result); // shows correct data
            return JSON.stringify(result); // how do I return this value so that any other file calling rsOffersAll gets THIS result?
}

async function rsOffersAll() {
    await global.MYDB.connect();
    try {
        global.MYDB.request(global.MYDB).query('SELECT Top(10) * FROM [Broadcast].[Offer]').then(extractResult);
    } catch (error) {
    }
}

module.exports.rsOffersAll = rsOffersAll;

Notice how rsOffersAll now contains no return statement.

ThisIsNoZaku
  • 2,213
  • 2
  • 26
  • 37
  • 1
    Appreciate the answer, but I didn't want another function to get the value from the original function. I needed to get the result out of the function which @stewartmcgown provided – volume one Nov 17 '19 at 19:49