├── .npmrc ├── .gitattributes ├── spec.emu ├── package.json ├── .gitignore ├── LICENSE ├── README.md └── index.html /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | index.html -diff merge=ours 2 | spec.js -diff merge=ours 3 | spec.css -diff merge=ours 4 | -------------------------------------------------------------------------------- /spec.emu: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
 7 | title: Async Initialization
 8 | stage: 1
 9 | contributors: Bradley Farias
10 | 
11 | 12 | 13 |

TBD

14 |
15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "template-for-proposals", 4 | "description": "A repository template for ECMAScript proposals.", 5 | "scripts": { 6 | "build": "ecmarkup spec.emu index.html" 7 | }, 8 | "homepage": "https://github.com/tc39/template-for-proposals#readme", 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/tc39/template-for-proposals.git" 12 | }, 13 | "license": "MIT", 14 | "devDependencies": { 15 | "ecmarkup": "^3.11.5" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | # Only apps should have lockfiles 40 | yarn.lock 41 | package-lock.json 42 | npm-shrinkwrap.json 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 ECMA TC39 and contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Async Initialization 2 | 3 | * Stage: 1 4 | * Champion: Bradley Farias (GoDaddy) 5 | 6 | ## Motivation 7 | 8 | Class features are unable to properly integrate with some async workflows in JS. 9 | This proposal seeks to aid async initialization of class instances. 10 | 11 | As stage 1, the current effort is into finding which design would be appealing 12 | given our goals. 13 | 14 | ## Goals 15 | 16 | * Encapsulate incomplete initialization 17 | * Allow a reasonable subclass workflow 18 | 19 | ## Potential Paths 20 | 21 | ### Async Classes 22 | 23 | Mark an entire class as `async` as well as the `constructor`: 24 | 25 | ```mjs 26 | async class Query { 27 | async constructor() { 28 | this.data = await performQuery('select * from table;'); 29 | } 30 | } 31 | ``` 32 | 33 | This is somewhat nice as it does open a path towards `await` during field 34 | initialization: 35 | 36 | ```mjs 37 | const connection = db.connect(); 38 | async class Table { 39 | db = await connection; 40 | query(q) { 41 | return db.query(q); 42 | } 43 | } 44 | ``` 45 | 46 | ### Async Constructors 47 | 48 | Only make `constructor` marked as `async`. 49 | 50 | ```mjs 51 | class Query { 52 | async constructor() { 53 | await performQuery('select * from table;') 54 | } 55 | } 56 | ``` 57 | 58 | This is minimal, but due to timing concerns. 59 | 60 | ### Private Constructors 61 | 62 | ```mjs 63 | class Query { 64 | #constructor(data) { 65 | this.data = data; 66 | } 67 | async create() { 68 | return new Query.prototype.#constructor( 69 | await performQuery('select * from table;') 70 | ); 71 | } 72 | } 73 | ``` 74 | 75 | This is also somewhat minimal, but does not allow for subclasses to easily 76 | extend the initialization since it is private. 77 | 78 | ## Common Problems Throughout 79 | 80 | All the solutions face some level of commonality for problems: 81 | 82 | ### `await super()` 83 | 84 | `await super()` is already valid so a new syntax would be needed in order to 85 | differentiate how `this` should be assigned. For bike shed mitigation, I will 86 | be using `await.super()` as a placeholder for now. It cannot be placed as a 87 | meta-property on `super` because `super.*` is also already valid syntax. 88 | 89 | ### Timing and resumption of super() 90 | 91 | Considering that many classes can perform meaningful actions prior to accessing 92 | superclass initialized behavior, it should be possible to queue such actions. 93 | 94 | ## FAQ 95 | 96 | What other languages support `await` during construction and encapsulate the instance until construction finishes? 97 | 98 | None found so far, but will happily document any. Lots of languages have various patterns for dealing with this problem space though. It seems somewhat common to have the following: 99 | 100 | ```mjs 101 | class Instance {} 102 | async function instanceFactory() { 103 | // awaits go here 104 | f(new Instance(data)); 105 | } 106 | ``` 107 | 108 | ## How will this interact with abstract sub/super classes? 109 | 110 | Unclear, but it seems that abstract classes would need to know timing. 111 | Timing is needed to avoid races such as performing an action too early and 112 | `async` initialization may affect the timing by which subclass `super` resumes. 113 | 114 | This leans towards banning mixing of `async` & non-`async` initialization to 115 | some extent. It seems unsafe for a non-`async` initialization to subclass an 116 | async initialization, but the opposite has not yet seen a problem. 117 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Async Initialization

Stage 1 Draft / February 11, 2020

Async Initialization

1824 | 1825 | 1826 |

1 TBD

1827 |
1828 |

A Copyright & Software License

1829 | 1830 |

Copyright Notice

1831 |

© 2020 Bradley Farias

1832 | 1833 |

Software License

1834 |

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

1835 | 1836 |

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1837 | 1838 |
    1839 |
  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 1840 |
  3. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  4. 1841 |
  5. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.
  6. 1842 |
1843 | 1844 |

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

1845 | 1846 |
1847 |
--------------------------------------------------------------------------------