0

I think at least one of the problems is that the rest of the code is processing while the function is still running. Here is some fleshed out code to show the problem. The first block is in an HTML file and uses load.js.

require(["load.js"], function(loader) {
    var load = new loader("fileLoad", "myID");
    var theString = load.loadFromDB();
    alert(theString);
});

With this code the variable 'theString' does not receive the returned value before the alert is called.

Here is the code from load.js:

define(["dojo/_base/declare", "dojo/request/xhr", "dojo/request"]
        , function(declare, xhr, request) {            

    return declare(null, {
        constructor: function(/*string*/ action, /*string*/ id) {
            this.action = action;
            this.id = id;
        },
        loadFromDB: function() {
            request.get("../../author-load-save.php", {
                query: {
                    action: this.action,
                    id: this.id
                }
            }).then(function(text) {
                console.log("The server returned: \n", text);                
                return text;                
            });
        }
    });
});
bstrong
  • 680
  • 9
  • 20
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Bergi Jan 07 '14 at 22:27

1 Answers1

2

You cannot return it, it's asynchronous. You can however return the promise that you are already using:

require(["load.js"], function(loader) {
    var load = new loader("fileLoad", "myID");
    load.loadFromDB().then(alert); // alert is a function that takes theString
});

define(["dojo/_base/declare", "dojo/request/xhr", "dojo/request"]
        , function(declare, xhr, request) {            

    return declare(null, {
        constructor: function(/*string*/ action, /*string*/ id) {
            this.action = action;
            this.id = id;
        },
        loadFromDB: function() {
            return request.get("../../author-load-save.php", {
//          ^^^^^^^
                query: {
                    action: this.action,
                    id: this.id
                }
            }).then(function(text) {
                console.log("The server returned: \n", text);                
                return text; 
            }); // makes a promise for the text
        }
    });
});
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • What do you mean, return the promise? I've seen references to promise and deferred but I don't understand them. Is it a way to get the value from the get request? – bstrong Jan 07 '14 at 22:56
  • 1
    It does provide a programmatic way to access the value that may arrive in the future, yes. – Bergi Jan 07 '14 at 22:59
  • Thank you. How can I access that value? When I try to print it I get this: [object Promise]. I also tried to print it inside of a timeout function to let it complete, but I get the same thing. – bstrong Jan 07 '14 at 23:15
  • 1
    Like I demonstrated, with the `then` method. Read more at http://dojotoolkit.org/reference-guide/1.9/dojo/promise.html – Bergi Jan 07 '14 at 23:21
  • Thank you, @Bergi. It is slow coming but making more sense. I was able to alert the returned value using Dojo's Deferred.when command http://dojotoolkit.org/reference-guide/1.7/dojo/when.html (for anyone else reading this I used "deferred.when(retVal, alert)", but of course yours will be different depending on what you call your Deferred function name in the require header). I'll have to look at it more, but I think I'm on a good track now. Thanks again, Berji. – bstrong Jan 07 '14 at 23:40