microsoft / microsoft/TypeScript
Suggestion: add flag to notify when awaiting a non-promise
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
async promise force error
Suggestion
Add a tsconfig.json flag to display info/warning/error when awaiting non-PromiseLike code. This will catch developer errors.
Use Cases
Catch developer errors when writing async code.
Example
The following code is valid:
await Promise.all(urls.map(url => {
fetch(url);
});
console.log('fetching complete');
The developer intends to do something once all the fetches complete, but since map is not returning any value, it ends up awaiting [undefined, undefined, ...], which means fetching complete is logged before the fetches complete. It would be nice if TS gave an info/warning/error in this case.
The correct code is:
await Promise.all(urls.map(url => {
return fetch(url);
});
console.log('fetching complete');
This example is paired down to the bare minimum for simplicity, but IRL code is more complex.
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Poiché nell’issue non sono identificati né un file sorgente né un test, inizia individuando la gestione delle espressioni await da parte di TypeScript e dei diagnostici di tsconfig. Il lavoro è completato quando è stato definito e implementato un info, warning o error configurabile per l’uso di await su codice che non è PromiseLike, con una copertura per l’esempio Promise.all fornito.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100