Skip to content

Commit ea415cd

Browse files
committed
fix(android): toJSONAsync fix
1 parent 20a74b6 commit ea415cd

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

src/https.android.ts

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,16 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
123123
return null;
124124
}
125125
}
126-
toStringAsync(): Promise<string> {
126+
async toStringAsync(): Promise<string> {
127127
if (this.stringResponse) {
128-
return Promise.resolve(this.stringResponse);
128+
return this.stringResponse;
129129
}
130130
// TODO: handle arraybuffer already stored
131-
return new Promise<string>((resolve, reject) => {
131+
this.stringResponse = await new Promise<string>((resolve, reject) => {
132132
this.getOrCreateCloseCallback();
133133
this.response.asStringAsync(this.getCallback(resolve, reject));
134-
}).then((r) => {
135-
this.stringResponse = r;
136-
return r;
137134
});
135+
return this.stringResponse;
138136
}
139137

140138
// cache it because asking it again wont work as the socket is closed
@@ -155,38 +153,25 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
155153
}
156154
}
157155

158-
toJSONAsync() {
156+
async toJSONAsync() {
159157
if (this.jsonResponse) {
160-
return Promise.resolve(this.jsonResponse);
158+
return this.jsonResponse;
161159
}
162-
return new Promise<string>((resolve, reject) => {
163-
if (this.stringResponse) {
164-
this.jsonResponse = Https.parseJSON(this.stringResponse);
165-
return this.jsonResponse;
166-
}
167-
// TODO: handle arraybuffer already stored
168-
return this.toStringAsync().then((r) => {
169-
this.jsonResponse = Https.parseJSON(this.stringResponse);
170-
return this.jsonResponse;
171-
});
172-
}).then(Https.parseJSON);
160+
if (this.stringResponse) {
161+
this.jsonResponse = Https.parseJSON(this.stringResponse);
162+
return this.jsonResponse;
163+
}
164+
// TODO: handle arraybuffer already stored
165+
const r = await this.toStringAsync();
166+
this.jsonResponse = Https.parseJSON(r);
167+
return this.jsonResponse;
173168
}
174-
// toImage(): Promise<ImageSource> {
175-
// return new Promise<any>((resolveImage, rejectImage) => {
176-
// try {
177-
// const image = this.response.toBitmap();
178-
// resolveImage(new ImageSource(image));
179-
// } catch (err) {
180-
// rejectImage(err);
181-
// }
182-
// });
183-
// }
184169

185170
// cache it because asking it again wont work as the socket is closed
186171
imageSource: ImageSource;
187-
toImage(): Promise<ImageSource> {
172+
async toImage(): Promise<ImageSource> {
188173
if (this.imageSource) {
189-
return Promise.resolve(this.imageSource);
174+
return this.imageSource;
190175
}
191176
return new Promise<ImageSource>((resolve, reject) => {
192177
this.getOrCreateCloseCallback();

0 commit comments

Comments
 (0)