Pnpjs: Chaining of batch add items

Created on 5 Jan 2021  路  4Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [ ] Bug
  • [x] Question
  • [ ] Documentation gap/issue

Version

Please specify what version of the library you are using: [ latest ]

Please specify what version(s) of SharePoint you are targeting: [Online]

Question

I am looking at the documentation to batch add items to a SharePoint list.

How does the chaining happen? Based on the code sample, list.items.inBatch(batch).add is a thenable (promise?), which means that batch.execute() will run without waiting for the add actions to be completed. What am I missing?

Code excerpt from the documentation:

list.getListItemEntityTypeFullName().then(entityTypeFullName => {

    let batch = sp.web.createBatch();

    list.items.inBatch(batch).add({ Title: "Batch 6" }, entityTypeFullName).then(b => {
        console.log(b);
    });

    list.items.inBatch(batch).add({ Title: "Batch 7" }, entityTypeFullName).then(b => {
        console.log(b);
    });

    batch.execute().then(d => console.log("Done"));
});

Thank you!

code question

All 4 comments

Hey Christophe!

An inBatch promise is resolved after batch.execute() is completed; the results are parsed out of a batch response and transferred to in batch promises. If do not provide batch.execute() an in batch promise is never resolved.

The same is relevant to in batch rejections. A batch can contain successful and failed responses in its body. PnPjs detects failures and rejects failed promises.

@koltyakov thanks for the explanation! I had no idea it was working that way around. Is it explained in the docs? I checked out the odata batch page before posting but didn't find much there.

I think what confused me is that execute() works differently in CSOM.

Where the docs are lacking we can always use help in getting them updated if you want to submit a PR to help clarify the spots you see gaps that would be a big help.

Will do!... as soon as I fully understand the behavior :-) I just wanted to make sure I didn't miss anything. I also plan to update the documentation following #1496.

I was doing a few validation tests yesterday with Promise.all. Unfortunately it seems that Promise.allSettled is not fully supported yet (I am working in SPFx).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simkessy picture simkessy  路  3Comments

simonagren picture simonagren  路  3Comments

KieranDaviesV picture KieranDaviesV  路  3Comments

ahsanranjha picture ahsanranjha  路  3Comments

SpliceVW picture SpliceVW  路  3Comments