0

I am creating a custom Dojo module that returns some data from a server. The problem is that I have an AJAX function within the module definition and I cannot return the outer function from the inner function when the request completes. If I simply return the outer function, it will return before I get a response from the server. I don't want to make the request synchronous (even though it works) because then I lose the benefit of the asynchronous module loading. I want other modules to be loaded at the same time as this one but I want it to wait for completion before returning.

What is the correct approach here? Should I be trying to return the outer function from the inner one? I've looked into this and can't seem to find anything.

Help is greatly appreciated!

define(["dojo/request","config/config"], function(Request, Config){
var imageTableId;
var req_opt = {handleAs:'json'};

Request.get(Config.RESTURL + "?f=json", req_opt).then(function(data){
    for (var i = 0; i < data.tables.length; i++){
        if (data.tables[i].name == "NotableTreeImageMap"){
            imageTableId = data.tables[i].id;
            return imageTableId;
        }
    }
},
function(error){
    console.log(error);
});

//return imageTableId;
});
NenadK
  • 381
  • 1
  • 6
  • 19
  • either accept a callback parameter, or return a promise object. See here for more info: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call It is impossible for you to return the imageTableId directly without going synchronous. – Kevin B Sep 05 '14 at 22:05
  • @Kevin OK that makes sense. I guess that leaves me no choice but not to do what I was trying to do in modules at all since I would need to load multiple synchronous modules asynchronously. Thanks for your help. I would upvote or accept the answer but I can't because of a lack of rep. – NenadK Sep 08 '14 at 15:46
  • I'm going to instead close it as a dupe of the linked when I get back to a computer, I think that would be more helpful for future visitors. – Kevin B Sep 08 '14 at 16:04

0 Answers0