Hacker News new | past | comments | ask | show | jobs | submit login

> Being able to store tasks/promises and reuse/await them later is a clear advantage

This sounds like horrible spaghetti. I can understand you might need to do it in exceptional circumstances, but I wouldn’t make a habit of it.




99% of the time you'll be storing promises in order to await on them in the same async method.

Those other 1% of times make for quite intuitive solutions, I think. For example, a cache. Most caches are request => already finished response, but with a lookup of promises you can easily do request => in-progress responses, for de-deplication.


Graceful shutdown & startup are two use cases that I used this for, just today.

When my express server gets a SIGTERM I want it to stop accepting new requests but finish pending requests before exiting.

Likewise during startup, the HTTP server is currently up before all resources are available - DB backends and the like. So I await a .ready promise before all requests iff we are not inited.

It can be abused into spaghetti for sure, but actual use cases are not that exotic imho




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: