forked from maxtaco/coffee-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
3602 lines (3087 loc) · 178 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>CoffeeScript</title>
<link rel="stylesheet" type="text/css" href="documentation/css/docs.css" />
<link rel="stylesheet" type="text/css" href="documentation/css/idle.css" />
<link rel="shortcut icon" href="documentation/images/favicon.ico" />
<script>
var __slice = Array.prototype.slice;
window.tame = {
Deferrals: (function() {
function _Class(_arg) {
this.continuation = _arg;
this.count = 1;
this.ret = null;
}
_Class.prototype._fulfill = function() {
if (!--this.count) return this.continuation(this.ret);
};
_Class.prototype.defer = function(defer_params) {
var _this = this;
++this.count;
return function() {
var inner_params, _ref;
inner_params = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
if (defer_params != null) {
if ((_ref = defer_params.assign_fn) != null) {
_ref.apply(null, inner_params);
}
}
return _this._fulfill();
};
};
return _Class;
})()
}
window.__tame_k = function() {};
</script>
</head>
<body>
<div id="fadeout"></div>
<div id="flybar">
<a id="logo" href="#top"> </a>
<div class="navigation toc">
<div class="button">
Table of Contents
</div>
<div class="contents menu">
<a href="#overview">Overview</a>
<a href="#installation">Installation</a>
<a href="#usage">Usage</a>
<a href="#language">Language Reference</a>
<a href="#tame">Tame Basic Features</a>
<a href="#tame_defer">Tame Defer Values</a>
<a href="#tame_expr">Tame Expressions</a>
<a href="#literals">Literals: Functions, Objects and Arrays</a>
<a href="#lexical_scope">Lexical Scoping and Variable Safety</a>
<a href="#conditionals">If, Else, Unless, and Conditional Assignment</a>
<a href="#splats">Splats...</a>
<a href="#loops">Loops and Comprehensions</a>
<a href="#slices">Array Slicing and Splicing</a>
<a href="#expressions">Everything is an Expression</a>
<a href="#operators">Operators and Aliases</a>
<a href="#classes">Classes, Inheritance, and Super</a>
<a href="#destructuring">Destructuring Assignment</a>
<a href="#fat_arrow">Function Binding</a>
<a href="#embedded">Embedded JavaScript</a>
<a href="#switch">Switch and Try/Catch</a>
<a href="#comparisons">Chained Comparisons</a>
<a href="#strings">String Interpolation, Heredocs, and Block Comments</a>
<a href="#regexes">Extended Regular Expressions</a>
<a href="#cake">Cake, and Cakefiles</a>
<a href="#scripts">"text/coffeescript" Script Tags</a>
<a href="#resources">Books, Screencasts, Examples and Resources</a>
<a href="#changelog">Change Log</a>
</div>
</div>
<div class="navigation try">
<div class="button">
Try CoffeeScript
<div class="repl_bridge"></div>
</div>
<div class="contents repl_wrapper">
<div class="code">
<div class="screenshadow tl"></div>
<div class="screenshadow tr"></div>
<div class="screenshadow bl"></div>
<div class="screenshadow br"></div>
<div id="repl_source_wrap">
<textarea id="repl_source" rows="100" spellcheck="false">alert "Hello CoffeeScript!"</textarea>
</div>
<div id="repl_results_wrap"><pre id="repl_results"></pre></div>
<div class="minibutton dark run" title="Ctrl-Enter">Run</div>
<a class="minibutton permalink" id="repl_permalink">Link</a>
<br class="clear" />
</div>
</div>
</div>
<div class="navigation annotated">
<div class="button">
Annotated Source
</div>
<div class="contents menu">
<a href="documentation/docs/grammar.html">Grammar Rules — src/grammar</a>
<a href="documentation/docs/lexer.html">Lexing Tokens — src/lexer</a>
<a href="documentation/docs/rewriter.html">The Rewriter — src/rewriter</a>
<a href="documentation/docs/nodes.html">The Syntax Tree — src/nodes</a>
<a href="documentation/docs/scope.html">Lexical Scope — src/scope</a>
<a href="documentation/docs/helpers.html">Helpers & Utility Functions — src/helpers</a>
<a href="documentation/docs/coffee-script.html">The CoffeeScript Module — src/coffee-script</a>
<a href="documentation/docs/cake.html">Cake & Cakefiles — src/cake</a>
<a href="documentation/docs/command.html">"coffee" Command-Line Utility — src/command</a>
<a href="documentation/docs/optparse.html">Option Parsing — src/optparse</a>
<a href="documentation/docs/repl.html">Interactive REPL — src/repl</a>
</div>
</div>
<div id="error" style="display:none;"></div>
</div>
<div class="container">
<span class="bookmark" id="top"></span>
<p>
CoffeeScript is a little language that compiles into JavaScript.
</p>
<p>
But mind you, this is a <b>fork</b> of CoffeeScript,
called <i>AdvancedCoffeeSubstitute (ACS)</i>.
<b>ACS is a drop-in replacement for CoffeeScript
designed to simplify and parallelize asynchronous control flow, both on the
browser, and in the server.</b>
</p>
<p>
The golden rule of CoffeeScript was: <i>"It's just JavaScript"</i>. ACS
is doing something deeper. It's selectively running code through a
<i>continuation-passing style conversion</i>. Original-style
CoffeeScript will compile as before, but code using the new ACS
features will be less recognizable. Still, the compiled output
is readable and pretty-printed, passes through
<a href="http://www.javascriptlint.com/">JavaScript Lint</a>
without warnings, will work in every JavaScript implementation, and tends
to run as fast or faster than the equivalent handwritten JavaScript.
</p>
<p>
<b>Latest Version:</b>
<a href="http://github.com/maxtaco/coffee-script/tarball/1.2.0">1.2.0</a>
</p>
<h2>
<span id="overview" class="bookmark"></span>
Overview
</h2>
<p><i>CoffeeScript on the left, compiled JavaScript output on the right.</i></p>
<div class='code'><pre class="idle"># Assignment:
number = 42
opposite = true
# Conditions:
number = -42 if opposite
# Functions:
square = (x) -> x * x
# Arrays:
list = [1, 2, 3, 4, 5]
# Objects:
math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
# Splats:
race = (winner, runners...) ->
print winner, runners
# Existence:
alert "I knew it!" if elvis?
# Array comprehensions:
cubes = (math.cube num for num in list)
</pre><pre class="idle"><span class="Storage">var</span> cubes, list, math, num, number, opposite, race, square,
__slice <span class="Keyword">=</span> <span class="LibraryClassType">Array</span>.<span class="LibraryConstant">prototype</span>.slice;
number <span class="Keyword">=</span> <span class="Number">42</span>;
opposite <span class="Keyword">=</span> <span class="BuiltInConstant">true</span>;
<span class="Keyword">if</span> (opposite) number <span class="Keyword">=</span> <span class="Keyword">-</span><span class="Number">42</span>;
<span class="FunctionName">square</span> = <span class="Storage">function</span>(<span class="FunctionArgument">x</span>) {
<span class="Keyword">return</span> x <span class="Keyword">*</span> x;
};
list <span class="Keyword">=</span> [<span class="Number">1</span>, <span class="Number">2</span>, <span class="Number">3</span>, <span class="Number">4</span>, <span class="Number">5</span>];
math <span class="Keyword">=</span> {
root: <span class="LibraryClassType">Math</span>.sqrt,
square: square,
<span class="FunctionName">cube</span>: <span class="Storage">function</span>(<span class="FunctionArgument">x</span>) {
<span class="Keyword">return</span> x <span class="Keyword">*</span> square(x);
}
};
<span class="FunctionName">race</span> = <span class="Storage">function</span>() {
<span class="Storage">var</span> runners, winner;
winner <span class="Keyword">=</span> arguments[<span class="Number">0</span>], runners <span class="Keyword">=</span> <span class="Number">2</span> <span class="Keyword"><=</span> arguments.<span class="LibraryConstant">length</span> ? __slice.<span class="LibraryFunction">call</span>(arguments, <span class="Number">1</span>) : [];
<span class="Keyword">return</span> <span class="LibraryFunction">print</span>(winner, runners);
};
<span class="Keyword">if</span> (<span class="Keyword">typeof</span> elvis <span class="Keyword">!</span><span class="Keyword">==</span> <span class="String"><span class="String">"</span>undefined<span class="String">"</span></span> <span class="Keyword">&</span><span class="Keyword">&</span> elvis <span class="Keyword">!</span><span class="Keyword">==</span> <span class="BuiltInConstant">null</span>) <span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>I knew it!<span class="String">"</span></span>);
cubes <span class="Keyword">=</span> (<span class="Storage">function</span>() {
<span class="Storage">var</span> _i, _len, _results;
_results <span class="Keyword">=</span> [];
<span class="Keyword">for</span> (_i <span class="Keyword">=</span> <span class="Number">0</span>, _len <span class="Keyword">=</span> list.<span class="LibraryConstant">length</span>; _i <span class="Keyword"><</span> _len; _i<span class="Keyword">++</span>) {
num <span class="Keyword">=</span> list[_i];
_results.<span class="LibraryFunction">push</span>(math.cube(num));
}
<span class="Keyword">return</span> _results;
})();
</pre><script>window.example1 = "# Assignment:\nnumber = 42\nopposite = true\n\n# Conditions:\nnumber = -42 if opposite\n\n# Functions:\nsquare = (x) -> x * x\n\n# Arrays:\nlist = [1, 2, 3, 4, 5]\n\n# Objects:\nmath =\n root: Math.sqrt\n square: square\n cube: (x) -> x * square x\n\n# Splats:\nrace = (winner, runners...) ->\n print winner, runners\n\n# Existence:\nalert \"I knew it!\" if elvis?\n\n# Array comprehensions:\ncubes = (math.cube num for num in list)\n\nalert cubes"</script><div class='minibutton ok' onclick='javascript: var cubes, list, math, num, number, opposite, race, square,
__slice = Array.prototype.slice;
number = 42;
opposite = true;
if (opposite) number = -42;
square = function(x) {
return x * x;
};
list = [1, 2, 3, 4, 5];
math = {
root: Math.sqrt,
square: square,
cube: function(x) {
return x * square(x);
}
};
race = function() {
var runners, winner;
winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return print(winner, runners);
};
if (typeof elvis !== "undefined" && elvis !== null) alert("I knew it!");
cubes = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = list.length; _i < _len; _i++) {
num = list[_i];
_results.push(math.cube(num));
}
return _results;
})();
;alert(cubes);'>run: cubes</div><br class='clear' /></div>
<h2>
<span id="installation" class="bookmark"></span>
Installation
</h2>
<p>
The CoffeeScript compiler is itself
<a href="documentation/docs/grammar.html">written in CoffeeScript</a>,
using the <a href="http://jison.org">Jison parser generator</a>. The
command-line version of <tt>coffee</tt> is available as a
<a href="http://nodejs.org/">Node.js</a> utility. The
<a href="extras/coffee-script.js">core compiler</a> however, does not
depend on Node, and can be run in any JavaScript environment, or in the
browser (see "Try CoffeeScript", above).
</p>
<p>
To install, first make sure you have a working copy of the latest stable version of
<a href="http://nodejs.org/">Node.js</a>, and <a href="http://npmjs.org">npm</a>
(the Node Package Manager). You can then install CoffeeScript with npm:
</p>
<pre>
npm install -g coffee-script</pre>
<p>
(Leave off the <tt>-g</tt> if you don't wish to install globally.)
</p>
<p>
If you'd prefer to install the latest master version of CoffeeScript, you
can clone the CoffeeScript
<a href="http://github.com/jashkenas/coffee-script">source repository</a>
from GitHub, or download
<a href="http://github.com/jashkenas/coffee-script/tarball/master">the source</a> directly.
To install the CoffeeScript compiler system-wide
under <tt>/usr/local</tt>, open the directory and run:
</p>
<pre>
sudo bin/cake install</pre>
<p>
If installing on Ubuntu or Debian,
<a href="http://opinionatedprogrammer.com/2010/12/installing-coffeescript-on-debian-or-ubuntu/">be
careful not to use the existing out-of-date package</a>.
</p>
<h2>
<span id="usage" class="bookmark"></span>
Usage
</h2>
<p>
Once installed, you should have access to the <tt>coffee</tt> command,
which can execute scripts, compile <tt>.coffee</tt> files into <tt>.js</tt>,
and provide an interactive REPL. The <tt>coffee</tt> command takes the
following options:
</p>
<table>
<tr>
<td><code>-c, --compile</code></td>
<td>
Compile a <tt>.coffee</tt> script into a <tt>.js</tt> JavaScript file
of the same name.
</td>
</tr>
<tr>
<td width="25%"><code>-i, --interactive</code></td>
<td>
Launch an interactive CoffeeScript session to try short snippets.
Identical to calling <tt>coffee</tt> with no arguments.
</td>
</tr>
<tr>
<td><code>-o, --output [DIR]</code></td>
<td>
Write out all compiled JavaScript files into the specified directory.
Use in conjunction with <tt>--compile</tt> or <tt>--watch</tt>.
</td>
</tr>
<tr>
<td><code>-j, --join [FILE]</code></td>
<td>
Before compiling, concatenate all scripts together in the order they
were passed, and write them into the specified file.
Useful for building large projects.
</td>
</tr>
<tr>
<td><code>-w, --watch</code></td>
<td>
Watch files for changes, rerunning the specified command when any
file is updated.
</td>
</tr>
<tr>
<td><code>-p, --print</code></td>
<td>
Instead of writing out the JavaScript as a file, print it
directly to <b>stdout</b>.
</td>
</tr>
<tr>
<td><code>-l, --lint</code></td>
<td>
If the <tt>jsl</tt>
(<a href="http://www.javascriptlint.com/">JavaScript Lint</a>)
command is installed, use it
to check the compilation of a CoffeeScript file. (Handy in
conjunction with <br /> <tt>--watch</tt>)
</td>
</tr>
<tr>
<td><code>-s, --stdio</code></td>
<td>
Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT.
Good for use with processes written in other languages. An example:<br />
<tt>cat src/cake.coffee | coffee -sc</tt>
</td>
</tr>
<tr>
<td><code>-e, --eval</code></td>
<td>
Compile and print a little snippet of CoffeeScript directly from the
command line. For example:<br /><tt>coffee -e "console.log num for num in [10..1]"</tt>
</td>
</tr>
<tr>
<td><code>-r, --require</code></td>
<td>
Load a library before compiling or executing your script. Can be used
to hook in to the compiler (to add Growl notifications, for example).
</td>
</tr>
<tr>
<td><code>-b, --bare</code></td>
<td>
Compile the JavaScript without the
<a href="#lexical_scope">top-level function safety wrapper</a>.
</td>
</tr>
<tr>
<td><code>-t, --tokens</code></td>
<td>
Instead of parsing the CoffeeScript, just lex it, and print out the
token stream: <tt>[IDENTIFIER square] [ASSIGN =] [PARAM_START (]</tt> ...
</td>
</tr>
<tr>
<td><code>-n, --nodes</code></td>
<td>
Instead of compiling the CoffeeScript, just lex and parse it, and print
out the parse tree:
<pre class="no_bar">
Expressions
Assign
Value "square"
Code "x"
Op *
Value "x"
Value "x"</pre>
</td>
</tr>
<tr>
<td><code>--nodejs</code></td>
<td>
The <tt>node</tt> executable has some useful options you can set,
such as<br /> <tt>--debug</tt>, <tt>--debug-brk</tt> and <tt>--max-stack-size</tt>. Use this
flag to forward options directly to Node.js.
</td>
</tr>
</table>
<p>
<b>Examples:</b>
</p>
<ul>
<li>
Compile a directory tree of <tt>.coffee</tt> files in <tt>src</tt> into a parallel
tree of <tt>.js</tt> files in <tt>lib</tt>:<br />
<tt>coffee --compile --output lib/ src/</tt>
</li>
<li>
Watch a file for changes, and recompile it every time the file is saved:<br />
<tt>coffee --watch --compile experimental.coffee</tt>
</li>
<li>
Concatenate a list of files into a single script:<br />
<tt>coffee --join project.js --compile src/*.coffee</tt>
</li>
<li>
Print out the compiled JS from a one-liner:<br />
<tt>coffee -bpe "alert i for i in [0..10]"</tt>
</li>
<li>
All together now, watch and recompile an entire project as you work on it:<br />
<tt>coffee -o lib/ -cw src/</tt>
</li>
<li>
Start the CoffeeScript REPL:<br />
<tt>coffee</tt>
</li>
</ul>
<h2>
<span id="language" class="bookmark"></span>
Language Reference
</h2>
<p>
<i>
This reference is structured so that it can be read from top to bottom,
if you like. <b>Early sections focus on new/forked ACS features</b>.
Later sections use ideas and syntax previously introduced.
Familiarity with JavaScript is assumed.
In all of the following examples, the source CoffeeScript is provided on
the left, and the direct compilation into JavaScript is on the right.
</i>
</p>
<p>
<i>
Many of the examples can be run (where it makes sense) by pressing the <b>run</b>
button on the right, and can be loaded into the "Try CoffeeScript"
console by pressing the <b>load</b> button on the left.
</i>
<p>
First, the basics: CoffeeScript uses significant whitespace to delimit blocks of code.
You don't need to use semicolons <tt>;</tt> to terminate expressions,
ending the line will do just as well (although semicolons can still
be used to fit multiple expressions onto a single line).
Instead of using curly braces
<tt>{ }</tt> to surround blocks of code in <a href="#functions">functions</a>,
<a href="#conditionals">if-statements</a>,
<a href="#switch">switch</a>, and <a href="#try">try/catch</a>,
use indentation.
</p>
<p>
You don't need to use parentheses to invoke a function if you're passing
arguments. The implicit call wraps forward to the end of the line or block expression.<br />
<tt>console.log sys.inspect object</tt> → <tt>console.log(sys.inspect(object));</tt>
</p>
<p>
<span id ="tame" class = "bookmark"></span>
<b class="header">Tame Basics</b>
Here is simple example that waits for 1 second, and then calls <tt>alert</tt>:
</p>
<p>
<div class='code'><pre class="idle">await setTimeout defer(), 1000
alert "back after a 1s delay"
</pre><pre class="idle"><span class="Storage">var</span> __tame_deferrals, __tame_k,
_this <span class="Keyword">=</span> <span class="Variable">this</span>;
<span class="FunctionName">__tame_k</span> = <span class="Storage">function</span>() {};
(<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
<span class="LibraryFunction">setTimeout</span>(__tame_deferrals.<span class="LibraryConstant">defer</span>({}), <span class="Number">1000</span>);
__tame_deferrals._fulfill();
})(<span class="Storage">function</span>() {
<span class="Keyword">return</span> <span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>back after a 1s delay<span class="String">"</span></span>);
});
</pre><script>window.example2 = "await setTimeout defer(), 1000\nalert \"back after a 1s delay\"\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class='minibutton ok' onclick='javascript: var __tame_deferrals, __tame_k,
_this = this;
__tame_k = function() {};
(function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
setTimeout(__tame_deferrals.defer({}), 1000);
__tame_deferrals._fulfill();
})(function() {
return alert("back after a 1s delay");
});
;'>run</div><br class='clear' /></div>
</p>
<p>
There is one new language addition here, the <tt>await { ... }</tt> block,
and also one new primitive
function, <tt>defer</tt>. The two of them work in concert. A
function must "wait" at the close of an await block until all
deferrals made in that await block are fulfilled. The
function <tt>defer</tt> returns a callback, and a callee in an
await block can fulfill a deferral by simply calling the callback
it was given. In the code above, there is only one deferral
produced, so after it's fulfilled by `setTimer` in 1000ms, control
continues past the `await` block, onto the alert. The code looks
and feels like threaded code, but is still in the asynchronous
idiom (if you look at the rewritten code output by
the <tt>coffee</tt> compiler).
</p>
<p>
The next example shows some of the power of the tame primitives.
In the following examples, there are two operations that fire in
parallel, and a third that fires after the first two complete:
</p>
<p>
<div class='code'><pre class="idle">window.slowAlert = (w,s,cb) ->
await setTimeout defer(), w
alert s
cb()
await
slowAlert 500, "hello", defer()
slowAlert 1000, "friend", defer()
await slowAlert 500, "back after a delay", defer()
</pre><pre class="idle"><span class="Storage">var</span> __tame_deferrals, __tame_k,
_this <span class="Keyword">=</span> <span class="Variable">this</span>;
<span class="FunctionName">__tame_k</span> = <span class="Storage">function</span>() {};
<span class="LibraryClassType">window</span>.<span class="FunctionName">slowAlert</span> = <span class="Storage">function</span>(<span class="FunctionArgument">w, s, cb</span>) {
<span class="Storage">var</span> __tame_deferrals,
_this <span class="Keyword">=</span> <span class="Variable">this</span>;
(<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
<span class="LibraryFunction">setTimeout</span>(__tame_deferrals.<span class="LibraryConstant">defer</span>({}), w);
__tame_deferrals._fulfill();
})(<span class="Storage">function</span>() {
<span class="LibraryFunction">alert</span>(s);
<span class="Keyword">return</span> cb();
});
};
(<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
slowAlert(<span class="Number">500</span>, <span class="String"><span class="String">"</span>hello<span class="String">"</span></span>, __tame_deferrals.<span class="LibraryConstant">defer</span>({}));
slowAlert(<span class="Number">1000</span>, <span class="String"><span class="String">"</span>friend<span class="String">"</span></span>, __tame_deferrals.<span class="LibraryConstant">defer</span>({}));
__tame_deferrals._fulfill();
})(<span class="Storage">function</span>() {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
slowAlert(<span class="Number">500</span>, <span class="String"><span class="String">"</span>back after a delay<span class="String">"</span></span>, __tame_deferrals.<span class="LibraryConstant">defer</span>({}));
__tame_deferrals._fulfill();
});
</pre><script>window.example3 = "window.slowAlert = (w,s,cb) ->\n await setTimeout defer(), w\n alert s\n cb()\nawait\n slowAlert 500, \"hello\", defer()\n slowAlert 1000, \"friend\", defer()\nawait slowAlert 500, \"back after a delay\", defer()\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example3);'>load</div><div class='minibutton ok' onclick='javascript: var __tame_deferrals, __tame_k,
_this = this;
__tame_k = function() {};
window.slowAlert = function(w, s, cb) {
var __tame_deferrals,
_this = this;
(function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
setTimeout(__tame_deferrals.defer({}), w);
__tame_deferrals._fulfill();
})(function() {
alert(s);
return cb();
});
};
(function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
slowAlert(500, "hello", __tame_deferrals.defer({}));
slowAlert(1000, "friend", __tame_deferrals.defer({}));
__tame_deferrals._fulfill();
})(function() {
__tame_deferrals = new tame.Deferrals(__tame_k);
slowAlert(500, "back after a delay", __tame_deferrals.defer({}));
__tame_deferrals._fulfill();
});
;'>run</div><br class='clear' /></div>
</p>
<p>
As one might expect, the tame constructs interoperate with standard
coffee control structures. The following program will call
<tt>slowAlert</tt> three times sequentially:
</p>
<p>
<div class='code'><pre class="idle">for i in [0..3]
await slowAlert 200, "loop iteration #{i}", defer()
</pre><pre class="idle"><span class="Storage">var</span> i, __tame_deferrals, _i, _next, _while,
_this <span class="Keyword">=</span> <span class="Variable">this</span>;
<span class="FunctionName">__tame_k</span> = <span class="Storage">function</span>() {};
i <span class="Keyword">=</span> <span class="Number">0</span>;
<span class="FunctionName">_while</span> = <span class="Storage">function</span>(<span class="FunctionArgument">__tame_k</span>) {
<span class="Storage">var</span> _break, _continue;
_break <span class="Keyword">=</span> __tame_k;
<span class="FunctionName">_continue</span> = <span class="Storage">function</span>() {
<span class="Keyword">++</span>i;
<span class="Keyword">return</span> _while(__tame_k);
};
_next <span class="Keyword">=</span> _continue;
<span class="Keyword">if</span> (i <span class="Keyword"><=</span> <span class="Number">3</span>) {
(<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
slowAlert(<span class="Number">200</span>, <span class="String"><span class="String">"</span>loop iteration <span class="String">"</span></span> <span class="Keyword">+</span> i, __tame_deferrals.<span class="LibraryConstant">defer</span>({}));
__tame_deferrals._fulfill();
})(_next);
} <span class="Keyword">else</span> {
<span class="Keyword">return</span> _break();
}
};
_while(__tame_k);
</pre><script>window.example4 = "for i in [0..3]\n await slowAlert 200, \"loop iteration #{i}\", defer()\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example4);'>load</div><div class='minibutton ok' onclick='javascript: var i, __tame_deferrals, _i, _next, _while,
_this = this;
__tame_k = function() {};
i = 0;
_while = function(__tame_k) {
var _break, _continue;
_break = __tame_k;
_continue = function() {
++i;
return _while(__tame_k);
};
_next = _continue;
if (i <= 3) {
(function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
slowAlert(200, "loop iteration " + i, __tame_deferrals.defer({}));
__tame_deferrals._fulfill();
})(_next);
} else {
return _break();
}
};
_while(__tame_k);
;'>run</div><br class='clear' /></div>
</p>
<p>
To fire those asynchronous calls in parallel, simply exchange
the <tt>for</tt> and <tt>await</tt> blocks:
</p>
<p>
<div class='code'><pre class="idle">await
for i in [0..3]
slowAlert 200, "loop iteration #{i}", defer()
</pre><pre class="idle"><span class="Storage">var</span> i, __tame_deferrals, __tame_k, _i;
<span class="FunctionName">__tame_k</span> = <span class="Storage">function</span>() {};
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
<span class="Keyword">for</span> (i <span class="Keyword">=</span> _i <span class="Keyword">=</span> <span class="Number">0</span>; _i <span class="Keyword"><=</span> <span class="Number">3</span>; i <span class="Keyword">=</span> <span class="Keyword">++</span>_i) {
slowAlert(<span class="Number">200</span>, <span class="String"><span class="String">"</span>loop iteration <span class="String">"</span></span> <span class="Keyword">+</span> i, __tame_deferrals.<span class="LibraryConstant">defer</span>({}));
}
__tame_deferrals._fulfill();
</pre><script>window.example5 = "await\n for i in [0..3]\n slowAlert 200, \"loop iteration #{i}\", defer()\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example5);'>load</div><div class='minibutton ok' onclick='javascript: var i, __tame_deferrals, __tame_k, _i;
__tame_k = function() {};
__tame_deferrals = new tame.Deferrals(__tame_k);
for (i = _i = 0; _i <= 3; i = ++_i) {
slowAlert(200, "loop iteration " + i, __tame_deferrals.defer({}));
}
__tame_deferrals._fulfill();
;'>run</div><br class='clear' /></div>
</p>
Things start to get interesting when you combine parallel and
serial control flow, but the syntax remains the same:
<p>
<div class='code'><pre class="idle">for i in [0..2]
await
slowAlert 100, "fast alert #{i}", defer()
slowAlert 200, "slow alert #{i}", defer()
</pre><pre class="idle"><span class="Storage">var</span> i, __tame_deferrals, _i, _next, _while,
_this <span class="Keyword">=</span> <span class="Variable">this</span>;
<span class="FunctionName">__tame_k</span> = <span class="Storage">function</span>() {};
i <span class="Keyword">=</span> <span class="Number">0</span>;
<span class="FunctionName">_while</span> = <span class="Storage">function</span>(<span class="FunctionArgument">__tame_k</span>) {
<span class="Storage">var</span> _break, _continue;
_break <span class="Keyword">=</span> __tame_k;
<span class="FunctionName">_continue</span> = <span class="Storage">function</span>() {
<span class="Keyword">++</span>i;
<span class="Keyword">return</span> _while(__tame_k);
};
_next <span class="Keyword">=</span> _continue;
<span class="Keyword">if</span> (i <span class="Keyword"><=</span> <span class="Number">2</span>) {
(<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
slowAlert(<span class="Number">100</span>, <span class="String"><span class="String">"</span>fast alert <span class="String">"</span></span> <span class="Keyword">+</span> i, __tame_deferrals.<span class="LibraryConstant">defer</span>({}));
slowAlert(<span class="Number">200</span>, <span class="String"><span class="String">"</span>slow alert <span class="String">"</span></span> <span class="Keyword">+</span> i, __tame_deferrals.<span class="LibraryConstant">defer</span>({}));
__tame_deferrals._fulfill();
})(_next);
} <span class="Keyword">else</span> {
<span class="Keyword">return</span> _break();
}
};
_while(__tame_k);
</pre><script>window.example6 = "for i in [0..2]\n await\n slowAlert 100, \"fast alert #{i}\", defer()\n slowAlert 200, \"slow alert #{i}\", defer()\n\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example6);'>load</div><div class='minibutton ok' onclick='javascript: var i, __tame_deferrals, _i, _next, _while,
_this = this;
__tame_k = function() {};
i = 0;
_while = function(__tame_k) {
var _break, _continue;
_break = __tame_k;
_continue = function() {
++i;
return _while(__tame_k);
};
_next = _continue;
if (i <= 2) {
(function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
slowAlert(100, "fast alert " + i, __tame_deferrals.defer({}));
slowAlert(200, "slow alert " + i, __tame_deferrals.defer({}));
__tame_deferrals._fulfill();
})(_next);
} else {
return _break();
}
};
_while(__tame_k);
;'>run</div><br class='clear' /></div>
</p>
<p>
<span id ="tame_defer" class = "bookmark"></span>
<b class="header">Tame Defer Values</b>
Many JS and coffee libraries use callbacks passed as parameters
to signify when the given operation has completed, and which values
were produced. As such, <tt>defer</tt> can take one or more arguments
which are filled in after a deferral is fulfilled. Note that the
snippet below is <i>node.js</i> code that requires a DNS resolver:
<p>
<div class='code'><pre class="idle">dns = require 'dns' # Use node.js's DNS resolution system
for host in [ 'yahoo.com', 'google.com', 'nytimes.com']
await dns.resolve host, "A", defer err, ip
if err then console.log "Error for #{host}: #{err}"
else console.log "Resolved #{host} -> #{ip}"
</pre><pre class="idle"><span class="Storage">var</span> dns, err, host, ip, __tame_deferrals, _i, _len, _next, _ref, _while,
_this <span class="Keyword">=</span> <span class="Variable">this</span>;
<span class="FunctionName">__tame_k</span> = <span class="Storage">function</span>() {};
dns <span class="Keyword">=</span> require(<span class="String"><span class="String">'</span>dns<span class="String">'</span></span>);
_ref <span class="Keyword">=</span> [<span class="String"><span class="String">'</span>yahoo.com<span class="String">'</span></span>, <span class="String"><span class="String">'</span>google.com<span class="String">'</span></span>, <span class="String"><span class="String">'</span>nytimes.com<span class="String">'</span></span>];
_len <span class="Keyword">=</span> _ref.<span class="LibraryConstant">length</span>;
_i <span class="Keyword">=</span> <span class="Number">0</span>;
<span class="FunctionName">_while</span> = <span class="Storage">function</span>(<span class="FunctionArgument">__tame_k</span>) {
<span class="Storage">var</span> _break, _continue;
_break <span class="Keyword">=</span> __tame_k;
<span class="FunctionName">_continue</span> = <span class="Storage">function</span>() {
<span class="Keyword">++</span>_i;
<span class="Keyword">return</span> _while(__tame_k);
};
_next <span class="Keyword">=</span> _continue;
<span class="Keyword">if</span> (_i <span class="Keyword"><</span> _len) {
host <span class="Keyword">=</span> _ref[_i];
(<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
dns.resolve(host, <span class="String"><span class="String">"</span>A<span class="String">"</span></span>, __tame_deferrals.<span class="LibraryConstant">defer</span>({
assign_fn: (<span class="Storage">function</span>() {
<span class="Keyword">return</span> <span class="Storage">function</span>() {
err <span class="Keyword">=</span> arguments[<span class="Number">0</span>];
<span class="Keyword">return</span> ip <span class="Keyword">=</span> arguments[<span class="Number">1</span>];
};
})()
}));
__tame_deferrals._fulfill();
})(<span class="Storage">function</span>() {
<span class="Keyword">return</span> _next(err ? <span class="TypeName">console</span><span class="LibraryFunction">.log</span>(<span class="String"><span class="String">"</span>Error for <span class="String">"</span></span> <span class="Keyword">+</span> host <span class="Keyword">+</span> <span class="String"><span class="String">"</span>: <span class="String">"</span></span> <span class="Keyword">+</span> err) : <span class="TypeName">console</span><span class="LibraryFunction">.log</span>(<span class="String"><span class="String">"</span>Resolved <span class="String">"</span></span> <span class="Keyword">+</span> host <span class="Keyword">+</span> <span class="String"><span class="String">"</span> -> <span class="String">"</span></span> <span class="Keyword">+</span> ip));
});
} <span class="Keyword">else</span> {
<span class="Keyword">return</span> _break();
}
};
_while(__tame_k);
</pre><script>window.example7 = "dns = require 'dns' # Use node.js's DNS resolution system\nfor host in [ 'yahoo.com', 'google.com', 'nytimes.com']\n await dns.resolve host, \"A\", defer err, ip\n if err then console.log \"Error for #{host}: #{err}\"\n else console.log \"Resolved #{host} -> #{ip}\"\n \n"</script><div class='minibutton load' onclick='javascript: loadConsole(example7);'>load</div><br class='clear' /></div>
</p>
<p>
<span id="tame_expr" class = "bookmark"></span>
<b class="header">Tame in an Expression Context</b>
It's possible
to use <tt>await</tt> blocks as expressions. And recursively
speaking, it's possible to use statements that contain `await`
blocks as expressions.
</p>
<p>
The simple rule is that an <tt>await</tt> block takes on the value of the
<tt>defer</tt> slot named <tt>_</tt> after its been fulfilled. If there
are multiple defer slots named <tt>_</tt> within the await block, then
the last writer wins. In practice, there's usually only one. Thus:
</p>
<p><div class='code'><pre class="idle">window.add = (a,b,cb) ->
await setTimeout defer(), 10
cb(a+b)
x = (await add 3, 4, defer _) + (await add 1, 2, defer _)
alert "#{x} is 10"
</pre><pre class="idle"><span class="Storage">var</span> x, __tame_deferrals,
_this <span class="Keyword">=</span> <span class="Variable">this</span>;
<span class="FunctionName">__tame_k</span> = <span class="Storage">function</span>() {};
<span class="LibraryClassType">window</span>.<span class="FunctionName">add</span> = <span class="Storage">function</span>(<span class="FunctionArgument">a, b, cb</span>) {
<span class="Storage">var</span> __tame_deferrals,
_this <span class="Keyword">=</span> <span class="Variable">this</span>;
(<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
<span class="LibraryFunction">setTimeout</span>(__tame_deferrals.<span class="LibraryConstant">defer</span>({}), <span class="Number">10</span>);
__tame_deferrals._fulfill();
})(<span class="Storage">function</span>() {
<span class="Keyword">return</span> cb(a <span class="Keyword">+</span> b);
});
};
(<span class="Storage">function</span>(__tame_k) {
<span class="Keyword">return</span> (<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
<span class="LibraryFunction">add</span>(<span class="Number">3</span>, <span class="Number">4</span>, __tame_deferrals.<span class="LibraryConstant">defer</span>({
assign_fn: (<span class="Storage">function</span>() {
<span class="Keyword">return</span> <span class="Storage">function</span>() {
<span class="Keyword">return</span> __tame_deferrals.ret <span class="Keyword">=</span> arguments[<span class="Number">0</span>];
};
})()
}));
__tame_deferrals._fulfill();
})(<span class="Storage">function</span>(___tame_p__2) {
<span class="Keyword">return</span> (<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
<span class="LibraryFunction">add</span>(<span class="Number">1</span>, <span class="Number">2</span>, __tame_deferrals.<span class="LibraryConstant">defer</span>({
assign_fn: (<span class="Storage">function</span>() {
<span class="Keyword">return</span> <span class="Storage">function</span>() {
<span class="Keyword">return</span> __tame_deferrals.ret <span class="Keyword">=</span> arguments[<span class="Number">0</span>];
};
})()
}));
__tame_deferrals._fulfill();
})(<span class="Storage">function</span>(___tame_p__1) {
<span class="Keyword">return</span> __tame_k(___tame_p__2 <span class="Keyword">+</span> ___tame_p__1);
});
});
})(<span class="Storage">function</span>(___tame_p__0) {
x <span class="Keyword">=</span> ___tame_p__0;
<span class="Keyword">return</span> <span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span><span class="String">"</span></span> <span class="Keyword">+</span> x <span class="Keyword">+</span> <span class="String"><span class="String">"</span> is 10<span class="String">"</span></span>);
});;
</pre><script>window.example8 = "window.add = (a,b,cb) ->\n await setTimeout defer(), 10\n cb(a+b)\nx = (await add 3, 4, defer _) + (await add 1, 2, defer _)\nalert \"#{x} is 10\"\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example8);'>load</div><div class='minibutton ok' onclick='javascript: var x, __tame_deferrals,
_this = this;
__tame_k = function() {};
window.add = function(a, b, cb) {
var __tame_deferrals,
_this = this;
(function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
setTimeout(__tame_deferrals.defer({}), 10);
__tame_deferrals._fulfill();
})(function() {
return cb(a + b);
});
};
(function(__tame_k) {
return (function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
add(3, 4, __tame_deferrals.defer({
assign_fn: (function() {
return function() {
return __tame_deferrals.ret = arguments[0];
};
})()
}));
__tame_deferrals._fulfill();
})(function(___tame_p__2) {
return (function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
add(1, 2, __tame_deferrals.defer({
assign_fn: (function() {
return function() {
return __tame_deferrals.ret = arguments[0];
};
})()
}));
__tame_deferrals._fulfill();
})(function(___tame_p__1) {
return __tame_k(___tame_p__2 + ___tame_p__1);
});
});
})(function(___tame_p__0) {
x = ___tame_p__0;
return alert("" + x + " is 10");
});;
;'>run</div><br class='clear' /></div></p>
<p>
Of course, things can get arbitrarily compicated and nested, so it
gets hairy. Consider this:
</p>
<p><div class='code'><pre class="idle">x = await add (await add 1, 2, defer _), (await add 3, 4, defer _), defer _
</pre><pre class="idle"><span class="Storage">var</span> x, __tame_deferrals,
_this <span class="Keyword">=</span> <span class="Variable">this</span>;
<span class="FunctionName">__tame_k</span> = <span class="Storage">function</span>() {};
(<span class="Storage">function</span>(__tame_k) {
(<span class="Storage">function</span>(__tame_k) {
__tame_deferrals <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">tame.Deferrals</span>(__tame_k);
<span class="LibraryFunction">add</span>(<span class="Number">1</span>, <span class="Number">2</span>, __tame_deferrals.<span class="LibraryConstant">defer</span>({
assign_fn: (<span class="Storage">function</span>() {
<span class="Keyword">return</span> <span class="Storage">function</span>() {
<span class="Keyword">return</span> __tame_deferrals.ret <span class="Keyword">=</span> arguments[<span class="Number">0</span>];
};