Best practices for promises?
- Lingua principale
- JavaScript
- Stelle
- 148k
- Fork
- 26.6k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
First of all, great job on this guide, I agree with it almost 100%. All of this should really be common knowledge, but I sure as hell didn't know most of these things when I started, so kudos for writing it all down.
Now, since promises have been standardized and a lot of people have started using them, it would be nice to have a section on how to keep promise-based code readable. For instance, when chaining multiple promises together, there are at least two alternative ways to do it:
#### Option 1: anonymous functions
``` javascript
return doSomething(foo).then(function(result) {
// ...
}).then(function(result2) {
// ...
}).then(function(result3) {
// ...
}).catch(function(error) {
// ...
}).finally(function() {
// ...
});
```
#### Option 2: named functions
``` javascript
function doThis(result) {
// ...
}
function doThat(result) {
// ...
}
function handleError(error) {
// ...
}
function done() {
// ...
}
return doSomething(foo)
.then(doThis)
.then(doThat)
.catch(handleError)
.finally(done);
```
The second way, while being somewhat more verbose, seems to be preferable, as the chain of tasks that the promise result travels through becomes more obvious.
I did an internal poll of our developers, and so far, option 2 seems to be ahead, although I have not heard from everyone yet. How does AirBnB deal with this, and what are your opinions?
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia leggendo la guida esistente e le sezioni relative a Promise, quindi esamina la discussione di questo issue sulle funzioni anonime rispetto a quelle con nome. Il lavoro è completato quando il progetto arriva a una decisione documentata e la guida contiene le indicazioni concordate sulla leggibilità.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100