Nodejs async function prototype chain error

why this code compiles

var Person =  function() {
console.log("CALLED PERSON")};

Person.prototype.saySomething = function() {
console.log("saySomething PERSON")};

var ape = new Person();
ape.saySomething();

and this code throws error Cannot set property ‘saySomething’ of undefined

var Person =  async function() {
console.log("CALLED PERSON")};

Person.prototype.saySomething = function() {
console.log("saySomething PERSON")};

var ape = new Person();
ape.saySomething();

3

When you use async function() {}, you are declaring an asynchronous function object. That’s different than a regular function object. The asynchronous function object does not have a prototype.

So, when you try to do this:

var Person =  async function() {
  console.log("CALLED PERSON")
};

Person.prototype.saySomething = function() {
  console.log("saySomething PERSON")
};

Person.prototype is undefined because there is no prototype on an asynchronous function object. Thus attempting to assign something to Person.prototype.saySomething causes the error you see because Person.prototype is undefined.

There is some logic to this because an asynchronous function can’t be used as a constructor because an asynchronous function always returns a promise so it can’t ever return a new object as in let obj = new f(). So, there’s no purpose in having a .prototype property because it can’t be used that way.

If you really wanted to asynchronously create an object, you could always create an async factory function that returns a promise that resolves with an object.

2

It is possible to add an async function in the end of the prototype chain.

Please notice that this works well in nodejs 8.11.1+.

// lets start with defining some cool async function who takes the time and 
// awaits a promise 

async function someCoolAsyncFunction() {
    // this let will be returned after timeout in a different value.
    let coolStuff = 'still boring';

    // do something cool which takes time 
    await new Promise((resolve, reject) => setTimeout(() => {
        coolStuff = 'an epiphany';
        resolve();
    }, 1000))
    return coolStuff;
}

// Now let's define the 'regular' prototype chain with it's boring functions.

function Person(p) { 
     this.constructorPropery = p;
     return this;
}

Person.prototype.notAsync = function() {
     // do something regular in the prototype chain
     console.log("Let's build some ",this.constructorPropery)
     this.kindOfPerson = 'Regular and boring'; 
     return this;   
}

// And now, lets add an async function to this chain

Person.prototype.someAsyncFunction = async function() {
    // you will still have access to 'this' in this function as long as the
    // previous function in the prototype chain returnes 'this'
    console.log('I used to be someone ',this.kindOfPerson);

    // Now, this is our time to shine, lets await something cool
    this.lifeChangingEvent = await someCoolAsyncFunction();
    console.log('Until I had ',this.lifeChangingEvent);
    a.kindOfPerson = 'enlightened';
    console.log('and now I am ', a.kindOfPerson);
    return this;
}

So this will work:

new Person('charachter').notAsync().someAsyncFunction();

But this WILL NOT work:

new Person('charachter').someAsyncFunction().notAsync();

And if you really need the data in ‘this’ outside the prototype chain you can also do:

let myself = new Person('charachter').notAsync();
console.log('myself.kindOfPerson is: ',myself.kindOfPerson);
myself.someAsyncFunction(); 
console.log('myself.kindOfPerson now is: ',myself.kindOfPerson);

Be sure to remember which prototype is an async function for which function or use your own naming convection for that.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật