Was ist der Vorteil einer "Versprechen" -Abstraktion in CommonJS?
ich leseDieser Beitrag und der Abschnitt über die Versprechungsabstraktion erscheint mir etwas zu kompliziert. Folgendes wird als Beispiel angegeben:
requestSomeData("http://example.com/foo") // returns a promise for the response
.then(function(response){ // ‘then’ is used to provide a promise handler
return JSON.parse(response.body); // parse the body
}) // returns a promise for the parsed body
.then(function(data){
return data.price; // get the price
}) // returns a promise for the price
.then(function(price){ // print out the price when it is fulfilled
print("The price is " + price);
});
Es scheint mir, dass das Folgende mit weniger Codezeilen dasselbe Ergebnis liefern könnte:
requestSomeData("http://example.com/foo")
.requestHandler(function(response){
// parse the body
var data = JSON.parse(response.body);
// get the price
var price = data.price;
// print out the price
print("The price is " + price);
});