proposal-async-init icon indicating copy to clipboard operation
proposal-async-init copied to clipboard

Versions of now usage

Open Dimtime opened this issue 5 years ago • 7 comments
trafficstars

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

Dimtime avatar May 23 '20 20:05 Dimtime

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

Dimtime avatar Aug 02 '20 03:08 Dimtime

@bmeck my last wrapper for me feels good only bad - this is not native version

Dimtime avatar Aug 02 '20 04:08 Dimtime

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 

Dimtime avatar Sep 08 '20 01:09 Dimtime

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 

Dimtime avatar Sep 08 '20 02:09 Dimtime

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.

ljharb avatar Sep 08 '20 04:09 ljharb

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

Dimtime avatar Sep 08 '20 18:09 Dimtime

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() );

Dimtime avatar Apr 30 '21 09:04 Dimtime