Binding addHandlerAsync won't work in parallel

Stan Calisie 0 Reputation points
2023-03-21T10:42:56.46+00:00

Greetings! I have a document with around 500 bindings. When my add-in loads it should add handlers for all bindings in the document. I call addHandlerAsync for each binding in an async loop. So, it calls the function around 500 times at the same time. But then I see how handlers are slowly attaching sequentially. Overall it takes about 1 to 3 minutes to attach all handlers. I don't call context.sync() between calls.
So, this affects user experience quite much, so I wonder if there is a workaround. Thank you!

Here's the code snippet:

 await each(bindings, ([bindingId, binding]) => {
      return new Promise((resolve, reject) => {
        binding.addHandlerAsync(
          Office.EventType.BindingDataChanged,
          this.handleBindingDataChanged,
          ({ status }) => {
            if (status === Office.AsyncResultStatus.Succeeded) {
              resolve(true);
            } else {
              reject(false);
            }
          }
        );
      });
    });
Microsoft 365 and Office | Development | Other
0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.