proposal-async-init
proposal-async-init copied to clipboard
Versions of now usage
Experimenting with async init now:
class PromiseClass {
static #New = async function(){ this.promise='promise'; return this;}
constructor(...args) {
return PromiseClass.#New.call(this,...args);
}//new
}//class
class AsyncClass extends PromiseClass{
static #New= async function(){this.async='async'; return this; }//#New
constructor(...args){
return (async()=>AsyncClass.#New.call(await super(),...args))();
}//new
}//AsyncClass
Or with private methods
class PromiseClass {
static async #New(){this.promise='promise'; return this;}
constructor(...args) {
return PromiseClass.#New.call(this,...args);
}//new
}//class
class AsyncClass extends PromiseClass{
static async #New(){this.async='async'; return this;}
constructor(...args){
return (async()=>AsyncClass.#New.call(await super(),...args))();
}//new
}//AsyncClass
Whoo! New version is ready: +more dynamic & universal standard (you dont need write class names - polyfill fill all for you) -lose private methods (cant execute private with this)
class PromiseClass {
static async $new(){this.promise='promise'; return this;}
constructor(...args) {return this.constructor.$new.call(this,...args);}//new
}//class
class AsyncClass extends PromiseClass{
static async $new(){super.$new(); this.async='async'; return this;}
constructor(...args){return super(...args)}//new
}//AsyncClass
@bmeck my last wrapper for me feels good only bad - this is not native version
Continue work...
class PromiseClass {
static $new=async(obj)=>{obj.promise='promise'; return obj;}
constructor(...args) {return this.constructor.$new(this,...args);}//new
}//class
class AsyncClass extends PromiseClass{
static $new=async(obj)=>{super.$new(obj); obj.async='async'; return obj;}
constructor(...args){return super(...args)}//new
}//AsyncClass
And continue...
class PromiseClass {
static $new=async(obj)=>{obj.promise='promise'; return obj;}
constructor(...args) {
let Class = this.constructor; if (Class.__proto__.name) return this;
return Class.$new(this,...args);}//new
}//class
class AsyncClass extends PromiseClass{
static $new=async(obj)=>{ await super.$new(obj); obj.async='async'; return obj;}
constructor(...args){ super(); return this.constructor.$new(this,...args);}//new
}//AsyncClass
note that if you stick js after the opening triple-backticks, there'll be syntax highlighting, which is particularly helpful for readability when there's minimal spacing/indentation.
New version work like: 1) go sync tree 2) go async tree
class PromiseClass {
async $new(){this.promise='promise'; return this;}
constructor(...args) {
let Proto = this.constructor.__proto__;
return (Proto.name) ? this : this.$new(...args);
}//new
}//class
class AsyncClass extends PromiseClass{
async $new(){ await super.$new(); this.async='async'; return this;}
constructor(...args){ super();
let Proto = this.constructor.__proto__; let Super = super.constructor;
return (Proto!=Super) ? this : this.$new(...args);
}//new
}//AsyncClass
class SuperClass extends AsyncClass{
async $new(){ await super.$new(); this.super='super'; return this;}
constructor(...args){ super();
let Proto = this.constructor.__proto__; let Super = super.constructor;
return (Proto!=Super) ? this : this.$new(...args);
}//new
}//SuperClass
New version: less code, but without standard Create New
class PromiseClass {
static async new(...args){let o= new this(); await o.$new(...args); return o};
async $new(){this.promise='promise';}
}//class
class AsyncClass extends PromiseClass{
async $new(){ await super.$new(); this.async='async';}
}//AsyncClass
class SuperClass extends AsyncClass{
async $new(){ await super.$new(); this.super='super'; }
}//SuperClass
console.dir(await SuperClass.new() );