Javascript承诺链接,重复逻辑附加到同一项数组

Javascript承诺链接,重复逻辑附加到同一项数组,javascript,typescript,Javascript,Typescript,链的各个部分根据结构重复。例如,网站可以有两个子网站,每个子网站都有两个列表。这意味着最后一个链将触发四次。在执行了所有内部更改之后,在何处/如何获取模板对象,以便获得完整的模板集合 let promises = []; let templates:ITemplate[] = []; pnp.sp.web.webs.get().then(sites => { sites.forEach(site => { pnp.sp.site.

链的各个部分根据结构重复。例如,网站可以有两个子网站,每个子网站都有两个列表。这意味着最后一个链将触发四次。在执行了所有内部更改之后,在何处/如何获取模板对象,以便获得完整的模板集合

    let promises = [];
    let templates:ITemplate[] = [];

    pnp.sp.web.webs.get().then(sites => {
      sites.forEach(site => {
        pnp.sp.site.openWebById(site.Id).then(result => {
          result.web.lists.get().then(lists => {
            lists.forEach(list => {
              promises.push(result.web.lists.getByTitle(list.Title).items.get());
            });
            Promise.all(promises)
            .then((results) => {
               // This runs multiple times which makes sense but I need the final templates array
               results.forEach(items => {
                items.forEach(item => {            
                  let template:ITemplate= {
                    Title:item.Title,
                    ...
                    ...
                  };
                  templates.push(template);
                });

                // If I add function here it runs multiple times which is not the desired outcome
                doSomethingWithTemplates(templates);
              });
            })
          })
        })
      });
    })

// Then do something with templates item collection that is a single item array with all items

我相信你可以从这个问题中看出,链接承诺是有缺点的——也许下面使用
async/await
的类似方法会更清楚地说明如何解决你的问题

Async/await
有效地建立在承诺之上,并且仍然使用承诺。给你一个想法,如果你还不知道

async function insertName():Promise<ITemplate[]> {
    let promises = [];
    let templates:ITemplate[] = [];
    const sites = await pnp.sp.web.webs.get()
    sites.forEach(site => {
        const result = await pnp.sp.site.openWebById(site.Id)
        const lists = await result.web.lists.get()
        lists.forEach(list => {
            const toPush = await result.web.lists.getByTitle(list.Title).items.get()
            promises.push(toPush);
        })
        const results = await Promise.all(promises)
        results.forEach(items => {
            items.forEach(item => {            
                let template:ITemplate= {
                    Title:item.Title,
                    ...
                    ...
                };
                templates.push(template);
            });
            doSomethingWithTemplates(templates);
        });
    })
    return templates
}
const templates: ITemplate[] = await insertName()
异步函数insertName():Promise{
让承诺=[];
let模板:ITemplate[]=[];
const sites=wait pnp.sp.web.webs.get()
sites.forEach(site=>{
const result=wait pnp.sp.site.openWebById(site.Id)
const lists=wait result.web.lists.get()
lists.forEach(list=>{
const toPush=wait result.web.lists.getByTitle(list.Title.items.get())
承诺。推(顶推);
})
const results=等待承诺。全部(承诺)
results.forEach(项目=>{
items.forEach(item=>{
让模板:ITemplate={
标题:item.Title,
...
...
};
模板。推送(模板);
});
带模板的dosomething(模板);
});
})
返回模板
}
常量模板:ITemplate[]=await insertName()

我不得不向内部arrow函数中添加async,并放弃了Promissions数组。你的回答很准确。不用担心——很高兴它有帮助!