Promises are non-blocking. This means that when you invoke the asynchronous function, JavaScript does not wait for the promise to resolve. This is the whole purpose of promises: you can allow tasks to execute "in the background" (kind of) while JavaScript executes the rest of the script/function.
The side effect: If the browser/node runtime reaches the end of the script, the program will terminate, regardless of whether the Promise
is resolved or not.
If you want to block JavaScript from terminating, you will have to block the main thread. Here is a solution I just came up with (if anybody has a better solution, leave it in the comments):
async function nonBlocking() { ... }
const interval = 100; // Blocks the main thread in 100 millisecond interval
const blockingInterval = setInterval(() => undefined, 100)
nonBlocking().then(value => {
clearInterval(blockingInterval)
// ... Rest of code here
}).catch(err => {
clearInterval(blockingInterval)
// ... Handle error here
})