Promises rejected properly when loading wrong content

This commit is contained in:
Vladimir Enchev
2015-09-02 11:40:50 +03:00
committed by vakrilov
parent 4c82981430
commit 0ff8038742
3 changed files with 74 additions and 9 deletions

View File

@ -62,8 +62,14 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
toString: () => { return NSDataToString(data); },
toJSON: () => { return JSON.parse(NSDataToString(data)); },
toImage: () => {
return new Promise<imageSource.ImageSource>((resolveImage, reject) => {
resolveImage(imageSource.fromData(data));
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {
var img = imageSource.fromData(data);
if (img instanceof imageSource.ImageSource) {
resolveImage(img);
} else {
rejectImage(new Error("Response content may not be converted to an Image"));
}
});
}
},