<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>The Longest Path</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/willduff/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/willduff/feed.xml</link>
    <description>A journey along a directed acyclic graph called Life</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:53:02 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>WinJS Promise Patterns: Listening for A Single DOM Event</title>
      <link>https://docs.microsoft.com/archive/blogs/willduff/winjs-promise-patterns-listening-for-a-single-dom-event</link>
      <pubDate>Thu, 29 Aug 2013 17:34:40 GMT</pubDate>
      <dc:creator><![CDATA[WillDuff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/willduff/2013/08/29/winjs-promise-patterns-listening-for-a-single-dom-event/</guid>
      <description><![CDATA[Firing DOM events is the most common way for JavaScript/HTML components to send notifications to...]]></description>
      <content:encoded><![CDATA[<p>Firing DOM events is the most common way for JavaScript/HTML components to send notifications to each other. If you are building a Windows Store app with JavaScript, you’ll likely need to mix asynchronous calls to Windows APIs with other logic that is waiting on DOM events. This can get messy and repetitive if you try to manage DOM event listeners manually. But waiting for a DOM event is asynchronous anyway, so we can wrap it in a promise.</p> <script src="https://gist.github.com/anonymous/6379891.js"></script>  <p>Wrapping it in a promise abstracts away the calls to addEventListener and removeEventListener, and allows us to compose this type of action with other promises. For example, let’s assume we want to insert an &lt;img&gt; element and then wait for it to load. Loading an image can take on the order of seconds depending on the size and location, so waiting for the load needs to be an asynchronous action or it will hang your app. </p>  <p>An HTML &lt;img&gt; will either load successfully and fire the <strong>load</strong> event, or it might fail to load and fire the <strong>error</strong> event. We only need to wait for one of these events to fire, so we can use <a href="http://msdn.microsoft.com/en-us/library/windows/apps/br229660.aspx" target="_blank">WinJS.Promise.any()</a> and our <strong>waitForSingleEventAsync()</strong> function above to create a promise that will either be fulfilled with the loaded image element or it will fail with the error “<em>Image load failed”</em>. Additionally, if the image load is already complete, we just return immediately.</p> <script src="https://gist.github.com/anonymous/6380094.js"></script>]]></content:encoded>
    </item>
    <item>
      <title>WinJS Promise Pitfalls</title>
      <link>https://docs.microsoft.com/archive/blogs/willduff/winjs-promise-pitfalls</link>
      <pubDate>Tue, 20 Aug 2013 22:07:24 GMT</pubDate>
      <dc:creator><![CDATA[WillDuff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/willduff/2013/08/20/winjs-promise-pitfalls/</guid>
      <description><![CDATA[I’ve been using WinJS promises in Windows Store apps for the past couple years. Async programming is...]]></description>
      <content:encoded><![CDATA[<p>I’ve been using WinJS promises in Windows Store apps for the past couple years. Async programming is much different than the standard synchronous, imperative programming most of us are used to. Here are a few lessons I’ve learned about using WinJS Promises, although these lessons could apply to any implementation of the <a href="http://wiki.commonjs.org/wiki/Promises/A" target="_blank">Common JS Promises/A spec</a>.</p>  <h3>Missed Catch</h3>  <p>The <a href="http://msdn.microsoft.com/en-us/library/windows/apps/br229728.aspx" target="_blank">then()</a> method takes up to 3 parameters – onComplete, onError and onProgress. But what happens if an exception is thrown in the onComplete handler?</p> <script src="https://gist.github.com/willduff/6289164.js"></script>  <p>Well, as specified in the Common JS Promises/A spec, the return value of then() is a promise in the error state. It does <strong>not </strong>call the onError handler. To handle an exception thrown by JSON.parse() we need to add an additional then(). The easiest way to do this is to provide a trailing then() that passes null for the onComplete handler.</p> <script src="https://gist.github.com/willduff/6289277.js"></script>  <h3>Not Done Yet</h3>  <p>As mentioned before, if an exception is thrown in the onComplete handler of then() the return value is a promise in the error state. But if there is no code to handle this error state then the error will silently go unhandled and your application will just keep running in a potentially invalid state.</p> <script src="https://gist.github.com/willduff/6290420.js"></script>  <p>The right thing to do is end each promise chain with <a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh701079.aspx" target="_blank">done()</a> which will re-throw any unhandled errors. If the error is unrecoverable, you can just pass no parameters to the done() method and it will re-throw the exception in the next turn of the event loop, likely forcing your Windows Store app to crash because there is no chance to catch the exception. Otherwise if your application can continue running after encountering the error, its in your best interest to at least log the error in order to make debugging easier. Other good options would be to assert, display an error message to the user and/or try to perform the action again. Like the previous example, the easiest way to do this is to provide a trailing done() that passes null for the onComplete handler.</p> <script src="https://gist.github.com/willduff/6290501.js"></script>  <h3>Indisposed</h3>  <p>In asynchronous programming, the state of the world can change right out from underneath you. An easy mistake to make is to tear down and dispose an object but to leave a promise inside it running. For example, let’s assume we want to show a Message each time the user performs an action. Additionally, we want to show just one message at time.</p> <script src="https://gist.github.com/willduff/6343179.js"></script>  <p>If the user is quick enough, he/she might perform two actions in quick succession:</p>  <ol>   <li>User performs Action A</li>    <li>Start showing Message A</li>    <li>User performs Action B</li>    <li>Dispose Message A (we only want to show one message at a time)</li>    <li>Start showing Message B</li> </ol>  <p>If step 4 causes Message A to be disposed before the animation has finished, it will cause a null reference exception when attempting to access this._element.dispatchEvent() (line 17) after the animation promise completes in showAsync() and calls fireEvent().</p>  <p>There are two potential ways to avoid this issue. The first is to store any promises in a member variable and then call <a href="http://msdn.microsoft.com/en-us/library/windows/apps/br211667.aspx" target="_blank">cancel()</a> on each one in dispose(). However, it turns out that WinJS animations support cancellation but swallow the cancellation and complete the animation promise, which means our then() following the animation still runs and we still get a null reference exception in the example above.</p>  <p>The alternative is to use a disposed flag inside your class and check the flag before proceeding. In this case, I’m returning the promise to the caller, so I cancel the promise if the class has been disposed the same way the cancel() method works – by returning a promise in the error state with the error message “Canceled”.</p> <script src="https://gist.github.com/willduff/6348524.js"></script>]]></content:encoded>
    </item>
    <item>
      <title>hello, world</title>
      <link>https://docs.microsoft.com/archive/blogs/willduff/hello-world</link>
      <pubDate>Sat, 08 Dec 2012 23:17:00 GMT</pubDate>
      <dc:creator><![CDATA[WillDuff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/willduff/2012/12/08/hello-world/</guid>
      <description><![CDATA[first!!!!111oneoneone]]></description>
      <content:encoded><![CDATA[<p>first!!!!111oneoneone</p>]]></content:encoded>
    </item>
  </channel>
</rss>