Ich habe folgenden Dienst in TypeScript, die Daten aus dem Back-End abruft.Proper Promise Behandlung Erfolg und Fehler Callbacks
Als Parameter der Funktion getAllPropertiesByAppId habe ich einen Erfolg und Fehler Callback.
export class PropertyService implements IPropertyService {
/**
* Get all properties
*/
public getAllPropertiesByAppId(appliactionId: string, success: (properties: Array<IPropertyItem>) => void, error: (error: any) => void): void {
//set up createRequestStr and requestInit
fetch(createRequestStr, requestInit)
.then<IPropertyItem[]>((response: Response) => {
if (response.status===401) {
throw new UnAuthorizedException();
}
return response.json<IPropertyItem[]>();
})
.then((response: IPropertyItem[]) => {
success(response);
})
.catch((reason: any) => {
//error handling
}
});
}
Dann verwende ich diesen Dienst in meiner Aktion Schöpfer:
initProperties: (appId: string): ActionCreator => (dispatch: Redux.Dispatch, getState:() => IApplicationState) => {
"use strict";
console.log("ShoppingCart initProperties - Request all Property");
var service: IPropertyService = kernel.get<IPropertyService>("IPropertyService");
service.getAllPropertiesByAppId(appId, (properties: Array<IPropertyItem>): void => {
dispatch(new ShoppingCartPropertiesLoaded(appId, properties));
dispatch(new ShoppingCartPropertiesDone(appId, System.Init.Done));
}, (error: any): void => {
console.log("ShoppingCart initProperties - error:" + error);
dispatch(new ShoppingCartPropertiesDone(appId, System.Init.Error));
});
}
Also, wenn ich InitProperties Aktion Schöpfer nennen nennt es getAllPropertiesByAppId und wenn alles in Ordnung ist, werde ich Versand Aktionen ShoppingCartPropertiesLoaded und ShoppingCartPropertiesDone.
Ich habe einfache Komponente zu speichern, verbunden ist, und die Komponente Fehler auslösen, wenn die Render-Methode
export default class TotalPriceList extends React.Component<ITotalPriceListProps, void> {
public render(): JSX.Element {
throw 'SomeError';
}
}
Die nicht behandelte Ausnahme in der Anweisung Fang endet ausführt von holen.
Ich habe etwas verpasst, wie Versprechen richtig oder sogar besser zu beenden, wie Funktion/Callback als Anweisung aufrufen und Versprechen zu verlassen, um zu vermeiden Ausnahme Ausnahme von Callback in Catch-Anweisung von fetch?
Vielen Dank für Ihre Hilfe
perfekt verwenden, das ist genau das, was ich suchte! Danke Bergi! –