-1

I'm using the AWS JavaScript SDK to work with DynamoDB.

I wrote the following (simplified) function to get the tables in my DB -

var AWS = require('aws-sdk');
var db = new AWS.DynamoDB();
function getTables() {
        db.listTables(function(err, data) {
            console.log(data);
            return data;
        });
    }

I can't understand why when I do the following -

x = getTables();

x doesn't get initialized with data (stays undefined), but console.log(data) prints the actual result.

I realize it's a callback, but I successfully print the response! It's not like I'm trying to print it outside the function, which would result in me trying to print something that hasn't been initialized yet. The response I get initialized the data variable, meaning it should be both printed and then returned!

What am I missing here? Thanks in advance

thomas
  • 1,133
  • 1
  • 12
  • 31

1 Answers1

1

There's a couple issues. The first is that you're not returning from your getTables function. So x will always be undefined.

Second issue is that you're trying to return data from the callback function, which is only executed sometime in the future and will be returing to the DynamoDB library function that executed it. You have to do whatever you want to do with data inside the callback function that you pass to db.listTables. The reason you data gets logged to the console is because at that point data is defined.

Here's an example for you to look at. It tries to explain the inner workings of your database library.

var FakeDB = {
    listTables: function(callback){
        setTimeout(function(){
            // This is your callback here.
            callback(["table1", "table2"]); // This is your callback function, this is where you're returning data to. To some code in FakeDB, inaccessable to you.

            //If I were to use "var data = callback(...);", data would be whatever you return from your callback.

        }, 5000);

        return undefined; // This is what is getting returned to your function: nothing.
    }
}

var x;

x = FakeDB.listTables(function(data){
    // This is where data is accessible

});

// x==undefined because that's what the listTables function returns.
BAM5
  • 341
  • 2
  • 6
  • Ah, I totally missed that I'm not returning from getTables! However I still don't get the second part... Why does it matter? The "return data" part is only executed after the console.log, so why can't I return it to the getTables function, and then return it to "main' from there? (I couldn't manage to do that either...) – thomas Dec 04 '15 at 20:23
  • 1
    Because at that point it's no longer you calling the function. It's the DynamoDB calling the function you passed to it (at some arbitrary point in the future). Does that make more sense?I can write a little example to try and clarify – BAM5 Dec 04 '15 at 20:26
  • No :( I understand what you're saying, it's just that it doesn't explain why once "DynamoDB is done calling the function I passed it", it can't return its results back to getTables and from there back to my main scope – thomas Dec 04 '15 at 20:28
  • 1
    Alright, I added an example. You should look up call stacks. It seems like you can use a bit more experience before delving into a project like this. But experimenting is a good way to get there. – BAM5 Dec 04 '15 at 20:43