locked
Issue with Promises

    Question

  • Hi Guys,

    I have created a sample promise as follows:

    function addAsync(a, b) { returnnew WinJS.Promise(function (comp) {

    //calling the method

    var xx = SampleMethod();

    comp(xx);

    }); }

    Here, the issue is that "SampleMethod" takes some time to complete its operation. I want the SampleMethod to complete

    its execution and then the returned value "xx" needs to be returned.

    However, what is happening is that the control doesnt go inside the SampleMethod(). The sample method is called and

    immediately the next statement "comp(xx)" gets executed and the control is returned back to the calling program.

    My desire is that the the SampleMethod() should get executed first completely and then the control should come to comp(xx).

    Am I doing anything wrong?

    Any help would be appreciated.

    Regards,

    Saurabh

    Friday, September 14, 2012 5:13 AM

Answers

  • I think you need to restructure differently, something like this:

    function SampleMethod() {

    returnnew WinJS.Promise(function (comp) {

    // do stuff

    // xx = ...

    comp(xx)

    }

    }

    Depending on how you wanted to use SampleMethod(), you might then want this:

    SampleMethod().then(function(xx) {

    // xx is the result of samplemethod()

    // you can call addAsync(xx, xx)

    });

    • Proposed as answer by Dino He Tuesday, September 18, 2012 5:32 AM
    • Marked as answer by Dino He Monday, September 24, 2012 7:37 AM
    Saturday, September 15, 2012 6:13 AM

All replies

  • Does SampleMethod return a Promise, or does it return a value?
    Friday, September 14, 2012 3:39 PM
  • I think you need to restructure differently, something like this:

    function SampleMethod() {

    returnnew WinJS.Promise(function (comp) {

    // do stuff

    // xx = ...

    comp(xx)

    }

    }

    Depending on how you wanted to use SampleMethod(), you might then want this:

    SampleMethod().then(function(xx) {

    // xx is the result of samplemethod()

    // you can call addAsync(xx, xx)

    });

    • Proposed as answer by Dino He Tuesday, September 18, 2012 5:32 AM
    • Marked as answer by Dino He Monday, September 24, 2012 7:37 AM
    Saturday, September 15, 2012 6:13 AM
  • Hi Brian,

    SampleMethod returns a value. But inside the SampleMethod, I have used some other async method calls. Is that an issue?

    Regards,

    Saurabh

    Monday, September 17, 2012 5:14 AM
  • That is indeed an issue. If you're calling an asynchronous method (and if you care about its return value), you have to be asynchronous yourself (unless nobody cares about your return value). I would set your code up similar to the way "w a p" suggests; you'll have to use a lot of .then() chaining to make all your asynchronous calls play nicely with each other. That's the downside of asynchronous programming: the asynchronous methods tend to propagate themselves rapidly. If you're going to be doing asynchronous stuff, my guess is most of your methods will end up being asynchronous themselves.


    Monday, September 17, 2012 2:18 PM
  • Thanks a lot .... I shall modify the code accordingly and have a look......

    Thanks again Brian and "w a p".

    Tuesday, September 18, 2012 2:56 AM