<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Pausing JavaScript with async.js</title>
	<atom:link href="http://eligrey.com/blog/post/pausing-javascript-with-async-js/feed" rel="self" type="application/rss+xml" />
	<link>http://eligrey.com/blog/post/pausing-javascript-with-async-js</link>
	<description>this instanceof Whitty</description>
	<lastBuildDate>Sun, 29 Jan 2012 12:58:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: gfxmonk</title>
		<link>http://eligrey.com/blog/post/pausing-javascript-with-async-js/comment-page-1#comment-2304</link>
		<dc:creator>gfxmonk</dc:creator>
		<pubDate>Sun, 31 Jan 2010 16:29:42 +0000</pubDate>
		<guid isPermaLink="false">http://eligrey.com/blog/?p=400#comment-2304</guid>
		<description>Interesting stuff, as I&#039;ve recently been set onto the path of NarrativeJS and Strands in order to save the same sort of problems. They seemed wonderful at first, but I encountered a handful of bugs with both, and debugging was a nightmare. 
 
Having said that, I must admit that it took me a good day or so to actually wrap my head around what&#039;s actually going on here. I&#039;m still not sure if I&#039;m doing things oddly, but I thought I&#039;d share the function call descriptor generator that I find most natural: 
 
&lt;pre lang=&quot;javascript&quot; escaped=&quot;true&quot;&gt;Function.prototype.result = function() { 
    var self=this; 
    var wrapper = function(func_args, cb) { 
        func_args = Array.prototype.slice.call(func_args); 
        func_args = func_args.slice(); 
        func_args.push(cb); 
        async(self).apply(this, func_args); 
    } 
    return [wrapper, arguments]; 
}&lt;/pre&gt;
 
 
this allows me to just have all my functions be regular (non-async-aware) functions that expect one more argument than they&#039;re called with (the callback argument). Sample usage: 
 
&lt;pre lang=&quot;javascript&quot; escaped=&quot;true&quot;&gt;function add_one(arg, cb) { 
    cb(arg + 1); 
} 
 
function main() { 
    alert(&quot;1 + 1 = &quot; + (yield add_one.result(1))); 
} 
 
async(main)();&lt;/pre&gt;
 
The &lt;code&gt;to&lt;/code&gt; function-call-description generator does feel less awkward to write with, but it seems to require functions be available in the global scope, which wouldn&#039;t work for me. And this way, I don&#039;t have to write wrapper functions for everything that just uses a callback without being async-aware. it also wrapps everything in as async() call as it goes, so that it should correctly deal with a yield wherever in the stack it occurs. </description>
		<content:encoded><![CDATA[<p>Interesting stuff, as I&#039;ve recently been set onto the path of NarrativeJS and Strands in order to save the same sort of problems. They seemed wonderful at first, but I encountered a handful of bugs with both, and debugging was a nightmare. </p>
<p>Having said that, I must admit that it took me a good day or so to actually wrap my head around what&#039;s actually going on here. I&#039;m still not sure if I&#039;m doing things oddly, but I thought I&#039;d share the function call descriptor generator that I find most natural:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">Function</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">result</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> self<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> wrapper <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>func_args<span style="color: #339933;">,</span> cb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
        func_args <span style="color: #339933;">=</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>func_args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        func_args <span style="color: #339933;">=</span> func_args.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        func_args.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>cb<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        async<span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> func_args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span> 
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#91;</span>wrapper<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>this allows me to just have all my functions be regular (non-async-aware) functions that expect one more argument than they&#039;re called with (the callback argument). Sample usage:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> add_one<span style="color: #009900;">&#40;</span>arg<span style="color: #339933;">,</span> cb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    cb<span style="color: #009900;">&#40;</span>arg <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;1 + 1 = &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>yield add_one.<span style="color: #660066;">result</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
&nbsp;
async<span style="color: #009900;">&#40;</span>main<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <code>to</code> function-call-description generator does feel less awkward to write with, but it seems to require functions be available in the global scope, which wouldn&#039;t work for me. And this way, I don&#039;t have to write wrapper functions for everything that just uses a callback without being async-aware. it also wrapps everything in as async() call as it goes, so that it should correctly deal with a yield wherever in the stack it occurs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eli Grey</title>
		<link>http://eligrey.com/blog/post/pausing-javascript-with-async-js/comment-page-1#comment-2305</link>
		<dc:creator>Eli Grey</dc:creator>
		<pubDate>Sun, 31 Jan 2010 13:11:34 +0000</pubDate>
		<guid isPermaLink="false">http://eligrey.com/blog/?p=400#comment-2305</guid>
		<description>I like your idea, and will include something similar to it in async.js. Instead of extending Function&#039;s prototype, I&#039;m going to make the &lt;code&gt;to&lt;/code&gt; object a function that takes a function as an argument and returns a function call descriptor generator for it that assumes the last argument of the function passed to it is the callback.</description>
		<content:encoded><![CDATA[<p>I like your idea, and will include something similar to it in async.js. Instead of extending Function&#8217;s prototype, I&#8217;m going to make the <code>to</code> object a function that takes a function as an argument and returns a function call descriptor generator for it that assumes the last argument of the function passed to it is the callback.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

