JavaScript
js/no-floating-promises
Every promise is awaited, returned or given a .catch(); an async call is never left floating.
umiarkowanaręcznefe-javascript
Dlaczego
save() without await runs on its own: the code after it does not wait, a failure becomes an unhandled rejection that the user never sees, and the order of updates is left to chance. items.forEach(async (item) => ...) is the same mistake in a loop: forEach ignores the promises.
Poprawka
await the call (or return it), or attach .catch() that shows or reports the error. Use for...of with await when order matters and Promise.all or Promise.allSettled when it does not. Mark a deliberate fire-and-forget with void save() and handle its errors inside. In a TypeScript project, turn on the typed lint rule @typescript-eslint/no-floating-promises; it finds these with type information that a text check cannot have.
Przykłady
// bad
button.addEventListener('click', () => { save(form); close() })
// good
button.addEventListener('click', async () => {
try { await save(form); close() } catch (err) { showError(err) }
})Źródła
Fakty
- Identyfikator
js/no-floating-promises- Waga
- umiarkowana
- Sprawdzanie
- ręczne ocenia człowiek; raport podaje to jako listę kontrolną
- Dotyczy plików
jsxvuesvelteastro- Uczy jej skill
fe-javascriptJavaScript in the browser- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain js/no-floating-promises