Best practices for promises?
- Vorherrschende Sprache
- JavaScript
- Sterne
- 148k
- Forks
- 26.6k
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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?
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Rechercherichtung
Beginne damit, den bestehenden Leitfaden und seine Promise-bezogenen Abschnitte zu lesen, und sieh dir dann die Diskussion dieses Issues über anonyme und benannte Funktionen an. Fertig ist die Aufgabe, wenn das Projekt eine dokumentierte Entscheidung getroffen hat und der Leitfaden die vereinbarten Hinweise zur Lesbarkeit enthält.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100