└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Asynchronous "do expressions" 2 | 3 | ## Overview 4 | 5 | Building on 6 | [async functions](https://github.com/tc39/ecmascript-asyncawait) 7 | and the not-formally-proposed 8 | ["do expressions"](http://wiki.ecmascript.org/doku.php?id=strawman:do_expressions), 9 | "async do expressions" let you write asynchronous blocks in an 10 | expression context, returning the completion value (a Promise). 11 | 12 | This is useful when functions expect an asynchronous result (a Promise) 13 | as input, and the steps to produce that result can be expressed in line 14 | with the function call. 15 | 16 | Example: 17 | ```js 18 | fetchEvent.respondWith(async { 19 | var response = await fetch('https://example.com/resource'); 20 | var body = await response.text(); 21 | 'BEGIN---\n' + body + '\n---END\n'; 22 | }); 23 | ``` 24 | 25 | This is similar to an "immediately invoked async function expression", i.e.: 26 | 27 | ```js 28 | fetchEvent.respondWith((async () => { 29 | var response = await fetch('https://example.com/resource'); 30 | var body = await response.text(); 31 | return 'BEGIN---\n' + body + '\n---END\n'; 32 | })()); 33 | ``` 34 | 35 | Or more verbosely: 36 | 37 | ```js 38 | async function f() { 39 | var response = await fetch('https://example.com/resource'); 40 | var body = await response.text(); 41 | return 'BEGIN---\n' + body + '\n---END\n'; 42 | }; 43 | let p = f(); 44 | fetchEvent.respondWith(p); 45 | ``` 46 | 47 | ## Syntax 48 | 49 | ``` 50 | PrimaryExpression : 51 | ... 52 | AsyncBlockExpression 53 | 54 | AsyncBlockExpression : 55 | "async" [no LineTerminator here] Block 56 | 57 | ExpressionStatement : 58 | [lookahead ∉ {{, function, class, let [, async}] Expression; 59 | ``` 60 | 61 | ## Semantics 62 | 63 | TODO - static and runtime semantics 64 | 65 | * `var` hoisting behavior needs definition 66 | * `break`, `continue` and `return` need definition 67 | 68 | ## References 69 | 70 | * [async functions](https://github.com/tc39/ecmascript-asyncawait) 71 | * [do expressions strawman](http://wiki.ecmascript.org/doku.php?id=strawman:do_expressions) 72 | * TC39 meeting notes: [Do Expression](https://github.com/rwaldron/tc39-notes/blob/master/es6/2014-01/jan-29.md#do-expression) 73 | * es-discuss thread: [monadic extension to do-notation](https://esdiscuss.org/topic/monadic-extension-to-do-notation) 74 | 75 | --------------------------------------------------------------------------------