Node.js command line script sometimes does not terminate
Asked Answered
F

1

9

I have a node js script with an async main method. Sometimes the script terminates fine, other times it hangs.

const main = async () => {
    let updates = []
    // ... add a bunch of promises to updates
    await Promise.all(updates)
} 

main()

Does anyone know why this script sometimes might hang? It just doesn't terminate though it appears to have run to completion.

Fechter answered 17/10, 2018 at 18:14 Comment(2)
It's impossible to be certain based on what you provide here, but generally this happens if you fail to resolve or reject all your promises and/or don't call process.exit() appropriately.Triiodomethane
Yes this happens to me a lot--your async function inherently returns a promise even though nothing is explicitly returned. So if you add main().then(() => process.exit()) it should always terminate.Fadeout
A
12

Because your function is async, you need to explicitly terminate it when it finishes:

main().then(() => process.exit())
Anisometropia answered 24/11, 2020 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.