In your last code you are making a request outside any function (in the top level execution), so it’s executed when JavaScript file is processed.
Regardless I would change two things in your code.
- I would switch to asynchronous
fetchAPI - it’s much easier to reason about and it makes the code easier to understand - you should NEVER send sensitive information in the URL. You should send the password and e-mail as body or in the headers.