JavaScript将列表中的每个项目动态添加到页面后,将它们绑定到列表中的每个项目

JavaScript将列表中的每个项目动态添加到页面后,将它们绑定到列表中的每个项目,javascript,asynchronous,promise,async-await,Javascript,Asynchronous,Promise,Async Await,我有一段代码,它在我的generator.js文件中生成一个项目列表: var OfferRequest = function( domain ) { const List = { template: null, orItems: [], SetData( data ) { if (data) { this.data = data; } return this; }, G

我有一段代码,它在我的
generator.js
文件中生成一个项目列表:

var OfferRequest = function( domain ) { 


const List = {

    template: null,
    orItems: [],

    SetData( data ) {

        if (data) {
            this.data = data;
        }
        return this;
    },

    GenerateItems() {
        if (!this.data || !this.data.length) {
            
            Loader.RecordsLoaded( 'Nu au fost inregistrate formulare.' );

        } 
        else {

            this.template = Template.Get('List','orItemTemplate').html();

            Template.Get('List','orListContainer').empty();

            let delay = 0;

            $(this.data).each((k, r) => {
                    
                let tpl = this.template.replace('', ''); // trick to get a copy of the template

                $.each(r, (i, v) => {
                    tpl = tpl.replace(`[${i}]`, v);
                });

                setTimeout(() => {

                    Template.Get('List','orListContainer').append( tpl );       
                    Settings.Store.Add( r );
                
                }, delay+=25);

            });
        }

    },


    Append( data ) {
    // ...
    }


}




async function Init( data, prospect ) {
    await new Promise((resolve, reject) => {

        if (!prospect) reject('Invalid param [prospect]');

        List.SetData( data ).GenerateItems();
        Settings.ProspectId = prospect;

        resolve();

    });
}



return {
    Init,
    Loader,
    Add,
    OpenRequest,
    GetExpander: () => Expander,
    ContractValability: Settings.Contract.Valability,
    OpenInfo,
    SetUnFinished,
    UpdateOffersCount,
    Offers
}
}

在包含此文件的页面的某个地方,我有初始化和绑定事件的代码:

let OR = new OfferRequest( 'ee' );

OR.Init( response, postData.prospect ).then(() => {

    PLUGIN.applyButtonAction({ target: 'div.or-item', fn: OR.OpenRequest }); // this is called before the items have rendered to page

}).catch(error => Dash.Server.Error( error ));
问题是在
或.Init()
上,在执行
列表.GenerateItems()
之前解决了
承诺。
List.GenerateItems
内部有一个setTimeout,我不知道如何强制
async Init()
在解析
承诺之前等待内部的所有进程完成


有没有一种方法可以使用
async
wait
promise
来实现这一点?

听起来您应该在添加项目后调用该代码……使
GenerateItems retuns promise
然后在每个操作完成后解析该承诺。并在Init函数中调用为
wait List.SetData(data.GenerateItems()添加
等待列表.SetData(data).GenerateItems()Init
中的code>抛出一个
未捕获的引用错误:未定义OfferRequest
-我以前尝试过这一点..也许应该在某个地方使用
异步