When updating text while looping through Sub
╰ SubTextPart Object
A SubTextPart object represents a user-defined part of a document. using the given for
╰ Collection Object
╰ forEach Method
Consider the following document with 3 SubTextParts that have been inserted with the following code:
The goal is to replace all SubTextParts that begin with the string "My". The obvious way to loop through the collection of SubTextParts would be by utilizing the forEach method in order to compare the name and to select and replace the text:
The problem with this code is that each function in the forEach loop is executed synchronously and results in the following output:
The replaced text is inserted after the last found SubTextPart.
After the text has been replaced, the Promise is resolved successfully and the next SubTextPart can be checked. The following screenshot shows that the first two SubTextParts have been replaced successfully.