Skip to content

Commit

Permalink
Define Future.any, Future.every, and Future.some.
Browse files Browse the repository at this point in the history
  • Loading branch information
annevk committed Apr 5, 2013
1 parent 67de637 commit 6f518ec
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 24 deletions.
128 changes: 116 additions & 12 deletions Overview.src.html
Expand Up @@ -610,7 +610,7 @@ <h3>Introduction to the <code title>Future</code></h3>
<pre>fetchJSON("/user/posts").done(showPosts, showFailcat)</pre>

<p>Using static methods <span title=concept-future>futures</span> can be combined to
create new <span title=concept-futures>futures</span>, which allows for operating on the
create new <span title=concept-future>futures</span>, which allows for operating on the
results of several asynchronous requests at once:

<pre>Future.when(fetchJSON("/user/mario"), fetchJSON("/user/luigi")).done(
Expand Down Expand Up @@ -847,9 +847,8 @@ <h3>Futures API</h3>
[<span title=dom-Future>Constructor</span>(<span>FutureInit</span> <var title>init</var>)]
interface <dfn>Future</dfn> {
static <span>Future</span> <span title=dom-Future-any>any</span>(any... <var title>futures</var>);
static <span>Future</span> <span title=dom-Future-every>every</span>(any... <var title>futures</var>);
static <span>Future</span> <span title=dom-Future-some>some</span>(any... <var title>futures</var>);
static <span>Future</span> <span title=dom-Future-when>when</span>(any... <var title>futures</var>);
static <span>Future</span> <span title=dom-Future-isThenable>isThenable</span>(any <var title>future</var>);

<span>Future</span> <span title=dom-Future-then>then</span>(optional <span>AnyCallback</span>? <var title>acceptCallback</var> = null, optional <span>AnyCallback</span>? <var title>rejectCallback</var> = null);
<span>Future</span> <span title=dom-Future-catch>catch</span>(optional <span>AnyCallback</span>? <var title>rejectCallback</var> = null);
Expand Down Expand Up @@ -958,27 +957,132 @@ <h3>Futures API</h3>
a chain of <span title=concept-future>futures</span> as exceptions thrown by its callbacks
will reach <code title>window.onerror</code>.

<hr>

<p>To <dfn title=concept-future-apply>apply</dfn> an <var title>acceptCallback</var> and

This comment has been minimized.

Copy link
@domenic

domenic Apr 5, 2013

Member

Here was my initial confusion:

Apply(accept, reject, futures)

  • For each future in futures:
    • Let f = new Future
    • Let r = ResolverOf(f)
    • Run r.resolve(future)
    • Run f.then(accept, reject)

Why not just

  • For each future in futures:
    • Run future.then(accept, reject)

?

Then I realized that the former assimilates thenables, whereas the latter does not. It might help to factor out the thenable assimilation, so that the algorithm could be phrased as

  • For each thenable in thenables:
    • Let future = Assimilate(thenable)
    • Run future.then(accept, reject)
<var title>rejectCallback</var> to a list of <var title>futures</var>, run these steps
for each <var title>future</var> in <var title>futures</var>:

<ol>
<li><p>Let <var title>f</var> be a new <span title=concept-future>future</span>.

<li><p>Let <var title>r</var> be <var title>f</var>'s associated
<span title=concept-resolver>resolver</span>.

<li><p>Run <var title>r</var>'s <span title=concept-resolver-resolve>resolve</span> with
<var title>future</var>.

<li><p><span title=concept-future-append>Append</span> <var title>acceptCallback</var>
and <var title>rejectCallback</var> to <var title>f</var>.
</ol>

<p>The static <dfn title=dom-Future-any><code>any(<var>futures</var>)</code></dfn> method
must run these steps:

<ol class=XXX></ol>
<ol>
<li><p>Let <var title>f</var> be a new <span title=concept-future>future</span>.

<p>The static <dfn title=dom-Future-some><code>some(<var>futures</var>)</code></dfn>
<li><p>Let <var title>r</var> be <var title>f</var>'s associated
<span title=concept-resolver>resolver</span>.

<li><p>Return <var title>f</var>.

This comment has been minimized.

Copy link
@domenic

domenic Apr 5, 2013

Member

This is confusing to me as a JS developer, although I guess in specs it could be allowed to return without necessarily terminating the steps of the algorithm. shrug.


<li><p>If <var>futures</var> is the empty list, invoke <var title>r</var>'s
<span title=concept-resolver-resolve>resolve</span> with <code title>undefined</code>.

<li><p>Let <var title>accept</var> be a new

This comment has been minimized.

Copy link
@domenic

domenic Apr 5, 2013

Member

Shouldn't this be "resolve", not "accept"? the mismatch is confusing.

<span title=concept-future-callback>future callback</span> for <var title>r</var> and its
<span title=concept-resolver-resolve>resolve</span> algorithm.

<li><p>Let <var title>reject</var> be a new
<span title=concept-future-callback>future callback</span> for <var title>r</var> and its
<span title=concept-resolver-reject>reject</span> algorithm.

<li><p><span title=concept-future-apply>Apply</span> <var title>accept</var> and
<var title>reject</var> to <var title>futures</var>
</ol>


<p>The static <dfn title=dom-Future-every><code>every(<var>futures</var>)</code></dfn>
method must run these steps:

<ol class=XXX></ol>
<ol>
<li><p>Let <var title>f</var> be a new <span title=concept-future>future</span>.

<li><p>Let <var title>r</var> be <var title>f</var>'s associated
<span title=concept-resolver>resolver</span>.

<li><p>Return <var title>f</var>.

<li><p>If <var>futures</var> is the empty list, invoke <var title>r</var>'s
<span title=concept-resolver-resolve>resolve</span> with <code title>undefined</code>.

<li><p>Let <var title>values</var> be an empty list.

<li>
<p>Let <var title>accept</var> be a JavaScript <code title>Function</code> Object which
when called runs these steps:

<ol>
<li><p>Let <var title>value</var> be the first argument that is passed, and
<code title>undefined</code> otherwise.

<li><p>Append <var title>value</var> to <var title>values</var>.

<li><p>If the number of <var title>values</var> equals the number of
<var title>futures</var>, run <var title>r</var>'s
<span title=concept-resolver-resolve>resolve</span> with <var title>values</var> and
the <i title>synchronous flag</i> set.
</ol>

<li><p>Let <var title>reject</var> be a new
<span title=concept-future-callback>future callback</span> for <var title>r</var> and its
<span title=concept-resolver-reject>reject</span> algorithm.

<li><p><span title=concept-future-apply>Apply</span> <var title>accept</var> and
<var title>reject</var> to <var title>futures</var>
</ol>


<p>The static <dfn title=dom-Future-when><code>when(<var>futures</var>)</code></dfn>
<p>The static <dfn title=dom-Future-some><code>some(<var>futures</var>)</code></dfn>
method must run these steps:

<ol class=XXX></ol>
<ol>
<li><p>Let <var title>f</var> be a new <span title=concept-future>future</span>.

<p>The static
<dfn title=dom-Future-isThenable><code>isThenable(<var>futures</var>)</code></dfn> method
must run these steps:
<li><p>Let <var title>r</var> be <var title>f</var>'s associated
<span title=concept-resolver>resolver</span>.

<li><p>Return <var title>f</var>.

<li><p>If <var>futures</var> is the empty list, invoke <var title>r</var>'s
<span title=concept-resolver-resolve>resolve</span> with <code title>undefined</code>.

<li><p>Let <var title>accept</var> be a new
<span title=concept-future-callback>future callback</span> for <var title>r</var> and its
<span title=concept-resolver-resolve>resolve</span> algorithm.

<ol class=XXX></ol>
<li><p>Let <var title>values</var> be an empty list.

<li>
<p>Let <var title>reject</var> be a JavaScript <code title>Function</code> Object which
when called runs these steps:

<ol>
<li><p>Let <var title>value</var> be the first argument that is passed, and
<code title>undefined</code> otherwise.

<li><p>Append <var title>value</var> to <var title>values</var>.

<li><p>If the number of <var title>values</var> equals the number of
<var title>futures</var>, run <var title>r</var>'s
<span title=concept-resolver-reject>reject</span> with <var title>values</var> and the
<i title>synchronous flag</i> set.
</ol>

<li><p><span title=concept-future-apply>Apply</span> <var title>accept</var> and
<var title>reject</var> to <var title>futures</var>
</ol>



Expand Down
128 changes: 116 additions & 12 deletions dom-core.html
Expand Up @@ -688,7 +688,7 @@ <h3 id="introduction-to-the-future"><span class="secno">4.1 </span>Introduction
<pre>fetchJSON("/user/posts").done(showPosts, showFailcat)</pre>

<p>Using static methods <a href="#concept-future" title="concept-future">futures</a> can be combined to
create new <span title="concept-futures">futures</span>, which allows for operating on the
create new <a href="#concept-future" title="concept-future">futures</a>, which allows for operating on the
results of several asynchronous requests at once:

<pre>Future.when(fetchJSON("/user/mario"), fetchJSON("/user/luigi")).done(
Expand Down Expand Up @@ -925,9 +925,8 @@ <h3 id="futures-api"><span class="secno">4.3 </span>Futures API</h3>
[<a href="#dom-future" title="dom-Future">Constructor</a>(<a href="#futureinit">FutureInit</a> <var title="">init</var>)]
interface <dfn id="future">Future</dfn> {
static <a href="#future">Future</a> <a href="#dom-future-any" title="dom-Future-any">any</a>(any... <var title="">futures</var>);
static <a href="#future">Future</a> <a href="#dom-future-every" title="dom-Future-every">every</a>(any... <var title="">futures</var>);
static <a href="#future">Future</a> <a href="#dom-future-some" title="dom-Future-some">some</a>(any... <var title="">futures</var>);
static <a href="#future">Future</a> <a href="#dom-future-when" title="dom-Future-when">when</a>(any... <var title="">futures</var>);
static <a href="#future">Future</a> <a href="#dom-future-isthenable" title="dom-Future-isThenable">isThenable</a>(any <var title="">future</var>);

<a href="#future">Future</a> <a href="#dom-future-then" title="dom-Future-then">then</a>(optional <a href="#anycallback">AnyCallback</a>? <var title="">acceptCallback</var> = null, optional <a href="#anycallback">AnyCallback</a>? <var title="">rejectCallback</var> = null);
<a href="#future">Future</a> <a href="#dom-future-catch" title="dom-Future-catch">catch</a>(optional <a href="#anycallback">AnyCallback</a>? <var title="">rejectCallback</var> = null);
Expand Down Expand Up @@ -1036,27 +1035,132 @@ <h3 id="futures-api"><span class="secno">4.3 </span>Futures API</h3>
a chain of <a href="#concept-future" title="concept-future">futures</a> as exceptions thrown by its callbacks
will reach <code title="">window.onerror</code>.

<hr>

<p>To <dfn id="concept-future-apply" title="concept-future-apply">apply</dfn> an <var title="">acceptCallback</var> and
<var title="">rejectCallback</var> to a list of <var title="">futures</var>, run these steps
for each <var title="">future</var> in <var title="">futures</var>:

<ol>
<li><p>Let <var title="">f</var> be a new <a href="#concept-future" title="concept-future">future</a>.

<li><p>Let <var title="">r</var> be <var title="">f</var>'s associated
<a href="#concept-resolver" title="concept-resolver">resolver</a>.

<li><p>Run <var title="">r</var>'s <a href="#concept-resolver-resolve" title="concept-resolver-resolve">resolve</a> with
<var title="">future</var>.

<li><p><a href="#concept-future-append" title="concept-future-append">Append</a> <var title="">acceptCallback</var>
and <var title="">rejectCallback</var> to <var title="">f</var>.
</ol>

<p>The static <dfn id="dom-future-any" title="dom-Future-any"><code>any(<var>futures</var>)</code></dfn> method
must run these steps:

<ol class="XXX"></ol>
<ol>
<li><p>Let <var title="">f</var> be a new <a href="#concept-future" title="concept-future">future</a>.

<p>The static <dfn id="dom-future-some" title="dom-Future-some"><code>some(<var>futures</var>)</code></dfn>
<li><p>Let <var title="">r</var> be <var title="">f</var>'s associated
<a href="#concept-resolver" title="concept-resolver">resolver</a>.

<li><p>Return <var title="">f</var>.

<li><p>If <var>futures</var> is the empty list, invoke <var title="">r</var>'s
<a href="#concept-resolver-resolve" title="concept-resolver-resolve">resolve</a> with <code title="">undefined</code>.

<li><p>Let <var title="">accept</var> be a new
<a href="#concept-future-callback" title="concept-future-callback">future callback</a> for <var title="">r</var> and its
<a href="#concept-resolver-resolve" title="concept-resolver-resolve">resolve</a> algorithm.

<li><p>Let <var title="">reject</var> be a new
<a href="#concept-future-callback" title="concept-future-callback">future callback</a> for <var title="">r</var> and its
<a href="#concept-resolver-reject" title="concept-resolver-reject">reject</a> algorithm.

<li><p><a href="#concept-future-apply" title="concept-future-apply">Apply</a> <var title="">accept</var> and
<var title="">reject</var> to <var title="">futures</var>
</ol>


<p>The static <dfn id="dom-future-every" title="dom-Future-every"><code>every(<var>futures</var>)</code></dfn>
method must run these steps:

<ol class="XXX"></ol>
<ol>
<li><p>Let <var title="">f</var> be a new <a href="#concept-future" title="concept-future">future</a>.

<li><p>Let <var title="">r</var> be <var title="">f</var>'s associated
<a href="#concept-resolver" title="concept-resolver">resolver</a>.

<li><p>Return <var title="">f</var>.

<li><p>If <var>futures</var> is the empty list, invoke <var title="">r</var>'s
<a href="#concept-resolver-resolve" title="concept-resolver-resolve">resolve</a> with <code title="">undefined</code>.

<li><p>Let <var title="">values</var> be an empty list.

<li>
<p>Let <var title="">accept</var> be a JavaScript <code title="">Function</code> Object which
when called runs these steps:

<ol>
<li><p>Let <var title="">value</var> be the first argument that is passed, and
<code title="">undefined</code> otherwise.

<li><p>Append <var title="">value</var> to <var title="">values</var>.

<li><p>If the number of <var title="">values</var> equals the number of
<var title="">futures</var>, run <var title="">r</var>'s
<a href="#concept-resolver-resolve" title="concept-resolver-resolve">resolve</a> with <var title="">values</var> and
the <i title="">synchronous flag</i> set.
</ol>

<li><p>Let <var title="">reject</var> be a new
<a href="#concept-future-callback" title="concept-future-callback">future callback</a> for <var title="">r</var> and its
<a href="#concept-resolver-reject" title="concept-resolver-reject">reject</a> algorithm.

<li><p><a href="#concept-future-apply" title="concept-future-apply">Apply</a> <var title="">accept</var> and
<var title="">reject</var> to <var title="">futures</var>
</ol>


<p>The static <dfn id="dom-future-when" title="dom-Future-when"><code>when(<var>futures</var>)</code></dfn>
<p>The static <dfn id="dom-future-some" title="dom-Future-some"><code>some(<var>futures</var>)</code></dfn>
method must run these steps:

<ol class="XXX"></ol>
<ol>
<li><p>Let <var title="">f</var> be a new <a href="#concept-future" title="concept-future">future</a>.

<p>The static
<dfn id="dom-future-isthenable" title="dom-Future-isThenable"><code>isThenable(<var>futures</var>)</code></dfn> method
must run these steps:
<li><p>Let <var title="">r</var> be <var title="">f</var>'s associated
<a href="#concept-resolver" title="concept-resolver">resolver</a>.

<li><p>Return <var title="">f</var>.

<li><p>If <var>futures</var> is the empty list, invoke <var title="">r</var>'s
<a href="#concept-resolver-resolve" title="concept-resolver-resolve">resolve</a> with <code title="">undefined</code>.

<li><p>Let <var title="">accept</var> be a new
<a href="#concept-future-callback" title="concept-future-callback">future callback</a> for <var title="">r</var> and its
<a href="#concept-resolver-resolve" title="concept-resolver-resolve">resolve</a> algorithm.

<ol class="XXX"></ol>
<li><p>Let <var title="">values</var> be an empty list.

<li>
<p>Let <var title="">reject</var> be a JavaScript <code title="">Function</code> Object which
when called runs these steps:

<ol>
<li><p>Let <var title="">value</var> be the first argument that is passed, and
<code title="">undefined</code> otherwise.

<li><p>Append <var title="">value</var> to <var title="">values</var>.

<li><p>If the number of <var title="">values</var> equals the number of
<var title="">futures</var>, run <var title="">r</var>'s
<a href="#concept-resolver-reject" title="concept-resolver-reject">reject</a> with <var title="">values</var> and the
<i title="">synchronous flag</i> set.
</ol>

<li><p><a href="#concept-future-apply" title="concept-future-apply">Apply</a> <var title="">accept</var> and
<var title="">reject</var> to <var title="">futures</var>
</ol>



Expand Down

0 comments on commit 6f518ec

Please sign in to comment.