Skip to content

Commit 1037816

Browse files
Snuffleupaguspull[bot]
authored andcommitted
Replace XMLHttpRequest usage with the Fetch API in send (in test/unit/testreporter.js)
Besides converting the `send` function to use the Fetch API, this patch also changes the method to return a `Promise` to get rid of the callback function. (Although, currently there's no call-site passing in a callback function.)
1 parent c8bc51f commit 1037816

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

test/unit/testreporter.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
const TestReporter = function (browser) {
2-
function send(action, json, cb) {
3-
const r = new XMLHttpRequest();
4-
// (The POST URI is ignored atm.)
5-
r.open("POST", action, true);
6-
r.setRequestHeader("Content-Type", "application/json");
7-
r.onreadystatechange = function sendTaskResultOnreadystatechange(e) {
8-
if (r.readyState === 4) {
9-
// Retry until successful
10-
if (r.status !== 200) {
11-
send(action, json, cb);
12-
} else {
13-
if (cb) {
14-
cb();
2+
function send(action, json) {
3+
return new Promise(resolve => {
4+
json.browser = browser;
5+
6+
fetch(action, {
7+
method: "POST",
8+
headers: {
9+
"Content-Type": "application/json",
10+
},
11+
body: JSON.stringify(json),
12+
})
13+
.then(response => {
14+
// Retry until successful.
15+
if (!response.ok || response.status !== 200) {
16+
throw new Error(response.statusText);
1517
}
16-
}
17-
}
18-
};
19-
json.browser = browser;
20-
r.send(JSON.stringify(json));
18+
resolve();
19+
})
20+
.catch(reason => {
21+
console.warn(`TestReporter - send failed (${action}): ${reason}`);
22+
resolve();
23+
24+
send(action, json);
25+
});
26+
});
2127
}
2228

2329
function sendInfo(message) {

0 commit comments

Comments
 (0)