Queue up async calls in sequence


  • I have a part in my metro app that fires "onChanged" events and I want to save some content to a file using 


    However, since this method runs async and the onChanged event is fired quicker than the writing occurs, I get Access Denied exceptions (File is in use by another process).

    Whats the recommended way to queue up such event handling?


    onChange: function() {
                  return WinJS.Application.local.writeText('test', JSON.stringify(_this.items)).then(function() {
                    return typeof console !== "undefined" && console !== null ? console.log("written") : void 0;

    Monday, February 6, 2012 2:01 PM


  • You can chain async calls like you are showing in your code snippet here.  I don't think that is your problem.  Are you asking how to serialize the onChange event so you can handle it?  If so, you will need to implement that yourself.  If you cannot respond fast enough to your change events you will need to push them onto some sort of stack or list and handle them during idle time or use some other similar scheme.

    I would question the architecture of your design if you need to write so many change events to a file.  It does not seem very efficient to me.  Perhaps you should consider some alternatives.  Maybe queue whatever information you need and periodically update it?  So much disk activity would also be a bit inefficient power wise as well.


    Jeff Sanders (MSFT)
    Monday, February 6, 2012 3:44 PM