Quickadd running templater code twice

What I’m trying to do

I’m trying to make a simple “smart paste” script - I have a templater script that I trigger from QuickAdd (capture macro that runs {{TEMPLATE:This Template}} that reads and parses my clipboard with some simple logic. I’m quite new to JavaScript.

A simplified version of the code is below:

<%*
clipboard = (await tp.system.clipboard());
if (clipboard.includes(Criteria 1)){
	tR += clipboard.replace('some text', 'some other text');
} else if (clipboard.includes(Criteria 2)) {
	tR += 'prefix' + clipboard;
} else if (clipboard.includes('**Criteria 3**')){
    title = (await tp.system.prompt('Title?'));
    tR += '[' + title + '](' + clipboard + ')';
} else {
	tR += '![](' + clipboard + ')';
} -%>

I’m having a ton of issues getting the third section (Criteria 3) working. I suspect this is some asynchronous race condition, but I can’t figure out how to fix it. When I run the above, one of two things happens:

  1. If I quickly type something in to the prompt that appears and hit enter, another prompt will appear immediately. If I then type in a title to the second prompt that appears, the code executes as expected and [TEXT I TYPED IN THE SECOND PROMPT](clipboard) appears in my note
  2. If I wait a few seconds and type something in to the prompt that appears, QuickAdd throws an error saying that the file has been modified since I last read it, implying that the script ran twice (consistent with 1).

Either way, for some reason it appears my script is running twice, but this is only happening when I add the second await, if I remove that line or replace it with title = 'some placeholder' the code runs just fine.

Debugging more - if I set up the templater script with no quickadd the code runs fully as expected, regardless of how long I spend on the prompt.