I'm getting an error when I try to cancel a previous PDFNetJS action


I’m using PDFNetJS Full with WebViewer, and want to allow adding bookmarks to a document being viewed. I also want to allow the user to cancel adding the bookmark. The bookmarks are a Bookmark object added to the PDF, and point to an Annotation.

Adding the bookmark works fine, but if I cancel, then try to add another one, I get the following error message.

A previous instance of pdfnet.beginoperation() has already been called without being terminated by pdfnet.finishoperation()


As the error indicates, you need to wait for one operation to complete, before starting another one.

In our sample code you can see that we wrap the main function call with runGeneratorWithCleanup, which is internally calling beginoperation and finishoperation for you.

What you want to do, if you want to support cancelling in your case, is use promise chaining.

In particular, you need to keep the promise returned by runGeneratorWithCleanup, and then for you cancel, and add bookmark again actions, chain your actions to previous ones, so they resolve in order.

Something similar to

previousOperationPromise = previousOperationPromise.then(function() { // Run new code });