Skip to content

Commit d438952

Browse files
committed
src: replace Promise chains with async/await
This helps with readability of the code. It also removed some Promises that were not not needed.
1 parent 467300e commit d438952

File tree

3 files changed

+94
-92
lines changed

3 files changed

+94
-92
lines changed

src/log.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -136,36 +136,35 @@ export class Log {
136136
requestInit.signal = controller.signal as AbortSignal;
137137
requestInit.method = 'GET';
138138

139-
await fetch(requestURL.toString(), requestInit)
140-
.then((response) => {
141-
const status = response.status;
142-
if (status === 200) {
143-
// TODO: the follow search param still has the stream close prematurely based on my testing
144-
response.body.pipe(stream);
145-
} else if (status === 500) {
146-
const v1status = response.body as V1Status;
147-
const v1code = v1status.code;
148-
const v1message = v1status.message;
149-
if (v1code !== undefined && v1message !== undefined) {
150-
throw new ApiException<undefined | V1Status>(
151-
v1code,
152-
v1message,
153-
v1status,
154-
normalizeResponseHeaders(response),
155-
);
156-
}
157-
} else {
158-
throw new ApiException<undefined>(
159-
status,
160-
'Error occurred in log request',
161-
undefined,
139+
try {
140+
const response = await fetch(requestURL.toString(), requestInit);
141+
const status = response.status;
142+
if (status === 200) {
143+
// TODO: the follow search param still has the stream close prematurely based on my testing
144+
response.body.pipe(stream);
145+
} else if (status === 500) {
146+
const v1status = response.body as V1Status;
147+
const v1code = v1status.code;
148+
const v1message = v1status.message;
149+
if (v1code !== undefined && v1message !== undefined) {
150+
throw new ApiException<undefined | V1Status>(
151+
v1code,
152+
v1message,
153+
v1status,
162154
normalizeResponseHeaders(response),
163155
);
164156
}
165-
})
166-
.catch((err) => {
167-
throw new ApiException<undefined>(err, 'Error occurred in log request', undefined, err);
168-
});
157+
} else {
158+
throw new ApiException<undefined>(
159+
status,
160+
'Error occurred in log request',
161+
undefined,
162+
normalizeResponseHeaders(response),
163+
);
164+
}
165+
} catch (err: any) {
166+
throw new ApiException<undefined>(err, 'Error occurred in log request', undefined, err);
167+
}
169168

170169
return controller;
171170
}

src/metrics.ts

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -88,44 +88,45 @@ export class Metrics {
8888
const requestInit = await this.config.applyToFetchOptions({});
8989
requestInit.method = 'GET';
9090

91-
return fetch(requestURL, requestInit)
92-
.then((response) => {
93-
return Promise.all([response.json(), response.status, response]);
94-
})
95-
.then(([json, status, response]) => {
96-
if (status === 200) {
97-
return json as T;
91+
try {
92+
const response = await fetch(requestURL, requestInit);
93+
const json = await response.json();
94+
const { status } = response;
95+
96+
if (status === 200) {
97+
return json as T;
98+
}
99+
100+
if (status === 500) {
101+
const v1status = json as V1Status;
102+
const v1code = v1status.code;
103+
const v1message = v1status.message;
104+
if (v1code !== undefined && v1message !== undefined) {
105+
throw new ApiException<undefined | V1Status>(
106+
v1code,
107+
v1message,
108+
v1status,
109+
normalizeResponseHeaders(response),
110+
);
98111
}
99-
if (status === 500) {
100-
const v1status = json as V1Status;
101-
const v1code = v1status.code;
102-
const v1message = v1status.message;
103-
if (v1code !== undefined && v1message !== undefined) {
104-
throw new ApiException<undefined | V1Status>(
105-
v1code,
106-
v1message,
107-
v1status,
108-
normalizeResponseHeaders(response),
109-
);
110-
}
111-
}
112-
throw new ApiException<undefined>(
113-
status,
114-
'Error occurred in metrics request',
115-
undefined,
116-
normalizeResponseHeaders(response),
117-
);
118-
})
119-
.catch((e) => {
120-
if (e instanceof ApiException) {
121-
throw e;
122-
}
123-
throw new ApiException<undefined | V1Status>(
124-
500,
125-
`Error occurred in metrics request: ${e.message}`,
126-
{},
127-
{},
128-
);
129-
});
112+
}
113+
114+
throw new ApiException<undefined>(
115+
status,
116+
'Error occurred in metrics request',
117+
undefined,
118+
normalizeResponseHeaders(response),
119+
);
120+
} catch (e: any) {
121+
if (e instanceof ApiException) {
122+
throw e;
123+
}
124+
throw new ApiException<undefined | V1Status>(
125+
500,
126+
`Error occurred in metrics request: ${e.message}`,
127+
{},
128+
{},
129+
);
130+
}
130131
}
131132
}

src/watch.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,36 @@ export class Watch {
5151
}
5252
};
5353

54-
await fetch(watchURL, requestInit)
55-
.then((response) => {
56-
if (response.status === 200) {
57-
response.body.on('error', doneCallOnce);
58-
response.body.on('close', () => doneCallOnce(null));
59-
response.body.on('finish', () => doneCallOnce(null));
54+
try {
55+
const response = await fetch(watchURL, requestInit);
6056

61-
const lines = createInterface(response.body);
62-
lines.on('error', doneCallOnce);
63-
lines.on('close', () => doneCallOnce(null));
64-
lines.on('finish', () => doneCallOnce(null));
65-
lines.on('line', (line) => {
66-
try {
67-
const data = JSON.parse(line.toString());
68-
callback(data.type, data.object, data);
69-
} catch (ignore) {
70-
// ignore parse errors
71-
}
72-
});
73-
} else {
74-
const error = new Error(response.statusText) as Error & {
75-
statusCode: number | undefined;
76-
};
77-
error.statusCode = response.status;
78-
throw error;
79-
}
80-
})
81-
.catch(doneCallOnce);
57+
if (response.status === 200) {
58+
response.body.on('error', doneCallOnce);
59+
response.body.on('close', () => doneCallOnce(null));
60+
response.body.on('finish', () => doneCallOnce(null));
61+
62+
const lines = createInterface(response.body);
63+
lines.on('error', doneCallOnce);
64+
lines.on('close', () => doneCallOnce(null));
65+
lines.on('finish', () => doneCallOnce(null));
66+
lines.on('line', (line) => {
67+
try {
68+
const data = JSON.parse(line.toString());
69+
callback(data.type, data.object, data);
70+
} catch (ignore) {
71+
// ignore parse errors
72+
}
73+
});
74+
} else {
75+
const error = new Error(response.statusText) as Error & {
76+
statusCode: number | undefined;
77+
};
78+
error.statusCode = response.status;
79+
throw error;
80+
}
81+
} catch (err) {
82+
doneCallOnce(err);
83+
}
8284

8385
return controller;
8486
}

0 commit comments

Comments
 (0)