0

I have a async function that should not execute another async request until the previous one has been finished.

pendingCatalogRequest = WinJS.Promise.as();
loadCatalogAsync = function(name) {
    var loadAsync = function() {
      return getXmlAsync("catalogdata/" + name);
    }
    return pendingCatalogRequest = pendingCatalogRequest.then(loadAsync, loadAsync);
}

Is this the correct way to handle this? Or am I missing something?

Will the loadAsync closure garbage collected when the loadCatalogAsync function is left?

philk
  • 2,009
  • 1
  • 23
  • 36

1 Answers1

0

Is this the correct way to handle this?

Yes, this seems fine.

Or am I missing something?

Consider what happens with rejections - they will block your whole queue. You might want to .catch them for the queuing promise. See here for a more complete example.

Will the loadAsync closure garbage collected when the loadCatalogAsync function is left?

No, it will get garbage collected after is is run, until then the pendingCatalogRequest.then method still holds a reference to it.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • > Consider what happens with rejections - they will block your whole queue. You might want to .catch them for the queuing promise. Am I not catching them with the error handler? > See here for a more complete example. The example is not WinJS unfortunately. – philk Apr 19 '15 at 18:38
  • Oops, I must have missed your error handler. The linked code should work when you swap jQuery's `$.when()` to `WinJS.Promise.as()`, the rest is implementation-independent. – Bergi Apr 19 '15 at 21:00
  • Still fail to see what your code does different than mine? Can you explain please? – philk Apr 20 '15 at 07:01
  • It won't store the latest xml result in the `pendingCatalogRequest`, which would always be a promise that is fulfilled with `undefined` only (in contrast to the promises returned from `loadCatalogAsync`) – Bergi Apr 20 '15 at 13:15