第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何等待構(gòu)造函數(shù)完成?

如何等待構(gòu)造函數(shù)完成?

瀟瀟雨雨 2023-04-20 09:56:19
我有一個(gè)具有異步元素的類構(gòu)造函數(shù)。稍后當(dāng)我創(chuàng)建此類的實(shí)例時(shí),我想讀取一個(gè)僅在構(gòu)造函數(shù)完成 100% 時(shí)才存在的屬性。我總是遇到問(wèn)題Can not read property 'id' of undefined.我?guī)缀蹩梢钥隙ㄟ@是關(guān)于異步的問(wèn)題..等待。    class NewPiecePlease {        constructor(IPFS, OrbitDB) {             this.OrbitDB = OrbitDB;                (async () => {                this.node = await IPFS.create();                        // Initalizing OrbitDB                this._init.bind(this);                this._init();            })();        }            // This will create OrbitDB instance, and orbitdb folder.        async _init() {            this.orbitdb = await this.OrbitDB.createInstance(this.node);            console.log("OrbitDB instance created!");                this.defaultOptions = { accessController: { write: [this.orbitdb.identity.publicKey] }}                const docStoreOptions = {                ...this.defaultOptions,                indexBy: 'hash',            }            this.piecesDb = await this.orbitdb.docstore('pieces', docStoreOptions);            await this.piecesDb.load();        }        ...   }后來(lái)我像這樣創(chuàng)建了這個(gè)類的一個(gè)實(shí)例:(async () => {    const NPP = new NewPiecePlease;    console.log(NPP.piecesDb.id);    // This will give 'undefined' error})();我如何告訴 NodeJS 我想new NewPiecePlease完全完成?await console.log(NPP.piecesDb.id);沒(méi)有幫助,這是可以理解的,因?yàn)樗粫?huì)理解我在等待什么。這樣做的正確方法是什么?
查看完整描述

1 回答

?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以為此使用工廠。它們非常適合進(jìn)行復(fù)雜的、潛在的異步對(duì)象創(chuàng)建,并使您的構(gòu)造函數(shù)保持干凈和專注。


 class NewPiecePlease {

  constructor(orbitdb, node, pieceDB) {

    this.orbitdb = orbitdb;

    this.node = node;

    this.pieceDB = pieceDB;

  }

  

  static async create(IPFS, OrbitDB) {

    const node = await IPFS.create();

    const orbitdb = await OrbitDB.createInstance(node);

    console.log("OrbitDB instance created!");


    const defaultOptions = {

      accessController: {

        write: [orbitdb.identity.publicKey]

      }

    }


    const docStoreOptions = { ...defaultOptions, indexBy: 'hash' };

    const piecesDb = await orbitdb.docstore('pieces', docStoreOptions);

    

    await piecesDb.load();

    

    return new NewPiecePlease(orbitdb, node, piecedb);

  }

}

如您所見(jiàn),create 方法執(zhí)行所有異步操作,并將結(jié)果傳遞給構(gòu)造函數(shù),在構(gòu)造函數(shù)中它實(shí)際上不需要執(zhí)行任何操作,除了分配和可能驗(yàn)證一些參數(shù)。


(async () => {

    const NPP = await NewPiecePlease.create(IPFS, OrbitDB);

    console.log(NPP.piecesDb.id);

    // This will give 'undefined' error

})();


查看完整回答
反對(duì) 回復(fù) 2023-04-20
  • 1 回答
  • 0 關(guān)注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)