0

I have this code,

MyNameSpace.Crate = {
    init: function (crateID) {
        var crate = MyNameSpace.Crate.get(crateID);
        MyNameSpace.Crate.processData(crate);
        // do more with data
    },
    get: function (crateID) {
        var url = root + "Crate/";
        $.getJSON(url, {
            crateID: crateID
        })
        .done(function (data) {
            // return data
        })
        .fail(function (data) {
            // do soemthing
        });;

    },
    processData : function(){
        // do something with data
    },
    so on..
}

What I want is to FIRST get data from MyNameSpace.Crate.get(crateID) only then move forward to MyNameSpace.Crate.processData(crate); and other methods. As you can see get method is async so I am not sure how can I force my code flow to get crate from get method before moving any forward.

Edit

I can call processData method in .done(... function but that is not I want to achieve, it will not let me follow object literal pattern, which is why question mentioned in comments is not helpful.

Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • @JuicyScripter they are not using object literal pattern – Mathematics Oct 14 '15 at 10:56
  • 1
    The pattern is irrelevant to the solution - pass the function to execute upon completion of the request in the callback. – Rory McCrossan Oct 14 '15 at 10:59
  • @RoryMcCrossan pattern isn't irrelevant here, I am trying to implement separation of concerns and separate methods as you can see, all of the solutions to question you are saying it's duplicate of are irrelevant, as I am already using callbacks "done".. – Mathematics Oct 14 '15 at 11:02
  • Pass an anonymous function to the `get()` method to be executed within the `done()`. In that anonymous function can be placed any code you require for that specific instance of `MyNameSpace.Crate`. By callback I am not referring to the `done()` method of the deferred. If you read and understand the explanation of the problem in the question I linked I'm sure this will become much clearer. – Rory McCrossan Oct 14 '15 at 11:03
  • @RoryMcCrossan i find it difficult to understand code in words, can you point me to the exact answer I could follow from the old question my post is duplicate of, please ? thanks – Mathematics Oct 14 '15 at 12:38
  • http://stackoverflow.com/a/14220323/519413 – Rory McCrossan Oct 14 '15 at 12:40
  • @RoryMcCrossan that question is like reading a book, even though i read it 10 times, it makes no sense to me, let me try once again – Mathematics Oct 14 '15 at 12:47

0 Answers0