JavaScript is not blocking and the next action runs right away

Hi there,
I'm trying to implement snippet manager like TextExpander.

When I type zst, it opens a prompt for text, I type "name" and it pastes

const [name, setName] = useState();

Everything seems fine, but when my JavaScript runs the applescript, the next action (Type Custom Text) just runs even before I type anything in the prompt.

I thought the next action would wait until I call returnToBTT() in the javascript. What did I do wrong here?