Skip to content

Commit 1246f0d

Browse files
authored
Convert readAsync to return a promise. NFC (#22035)
Followup to #22026 Since all the call sites are mostly promise based it makes logical sense to return a promise here.
1 parent c2b51cb commit 1246f0d

File tree

87 files changed

+134
-129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+134
-129
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions

src/library.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,19 +3119,22 @@ addToLibrary({
31193119
$asyncLoad__docs: '/** @param {boolean=} noRunDep */',
31203120
$asyncLoad: (url, onload, onerror, noRunDep) => {
31213121
var dep = !noRunDep ? getUniqueRunDependency(`al ${url}`) : '';
3122-
readAsync(url, (arrayBuffer) => {
3122+
readAsync(url).then(
3123+
(arrayBuffer) => {
31233124
#if ASSERTIONS
3124-
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
3125+
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
31253126
#endif
3126-
onload(new Uint8Array(arrayBuffer));
3127-
if (dep) removeRunDependency(dep);
3128-
}, (event) => {
3129-
if (onerror) {
3130-
onerror();
3131-
} else {
3132-
throw `Loading data file "${url}" failed.`;
3127+
onload(new Uint8Array(arrayBuffer));
3128+
if (dep) removeRunDependency(dep);
3129+
},
3130+
(err) => {
3131+
if (onerror) {
3132+
onerror();
3133+
} else {
3134+
throw `Loading data file "${url}" failed.`;
3135+
}
31333136
}
3134-
});
3137+
);
31353138
if (dep) addRunDependency(dep);
31363139
},
31373140

src/library_browser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,10 +802,10 @@ var LibraryBrowser = {
802802

803803
#if ENVIRONMENT_MAY_BE_NODE && DYNAMIC_EXECUTION
804804
if (ENVIRONMENT_IS_NODE) {
805-
readAsync(url, (data) => {
805+
readAsync(url, false).then((data) => {
806806
eval(data);
807807
loadDone();
808-
}, loadError, false);
808+
}, loadError);
809809
return;
810810
}
811811
#endif

src/node_shell_read.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ readBinary = (filename) => {
2222
return ret;
2323
};
2424

25-
readAsync = (filename, onload, onerror, binary = true) => {
25+
readAsync = (filename, binary = true) => {
2626
// See the comment in the `read_` function.
2727
filename = isFileURI(filename) ? new URL(filename) : nodePath.normalize(filename);
28-
fs.readFile(filename, binary ? undefined : 'utf8', (err, data) => {
29-
if (err) onerror(err);
30-
else onload(binary ? data.buffer : data);
28+
return new Promise((resolve, reject) => {
29+
fs.readFile(filename, binary ? undefined : 'utf8', (err, data) => {
30+
if (err) reject(err);
31+
else resolve(binary ? data.buffer : data);
32+
});
3133
});
3234
};

src/preamble.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -648,15 +648,12 @@ function getBinaryPromise(binaryFile) {
648648
|| isDataURI(binaryFile)
649649
#endif
650650
) {
651-
// Fetch the binary use readAsync
652-
return new Promise((resolve, reject) => {
653-
readAsync(binaryFile,
654-
(response) => resolve(new Uint8Array(/** @type{!ArrayBuffer} */(response))),
655-
(error) => {
656-
try { resolve(getBinarySync(binaryFile)); }
657-
catch (e) { reject(e); }
658-
});
659-
});
651+
// Fetch the binary using readAsync
652+
return readAsync(binaryFile).then(
653+
(response) => new Uint8Array(/** @type{!ArrayBuffer} */(response)),
654+
// Fall back to getBinarySync if readAsync fails
655+
() => getBinarySync(binaryFile)
656+
);
660657
}
661658
#endif
662659

src/shell.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,10 @@ if (ENVIRONMENT_IS_SHELL) {
324324
return data;
325325
};
326326

327-
readAsync = (f, onload, onerror) => {
328-
setTimeout(() => onload(readBinary(f)));
327+
readAsync = (f) => {
328+
return new Promise((resolve, reject) => {
329+
setTimeout(() => resolve(readBinary(f)));
330+
});
329331
};
330332

331333
if (typeof clearTimeout == 'undefined') {

src/web_or_worker_shell_read.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,35 @@
2121
};
2222
}
2323

24-
readAsync = (url, onload, onerror) => {
24+
readAsync = (url) => {
2525
#if ENVIRONMENT_MAY_BE_WEBVIEW
2626
// Fetch has some additional restrictions over XHR, like it can't be used on a file:// url.
2727
// See https://github.com/github/fetch/pull/92#issuecomment-140665932
2828
// Cordova or Electron apps are typically loaded from a file:// url.
2929
// So use XHR on webview if URL is a file URL.
3030
if (isFileURI(url)) {
31-
var xhr = new XMLHttpRequest();
32-
xhr.open('GET', url, true);
33-
xhr.responseType = 'arraybuffer';
34-
xhr.onload = () => {
35-
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
36-
onload(xhr.response);
37-
return;
38-
}
39-
onerror();
40-
};
41-
xhr.onerror = onerror;
42-
xhr.send(null);
43-
return;
31+
return new Promise((reject, resolve) => {
32+
var xhr = new XMLHttpRequest();
33+
xhr.open('GET', url, true);
34+
xhr.responseType = 'arraybuffer';
35+
xhr.onload = () => {
36+
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
37+
resolve(xhr.response);
38+
}
39+
reject(xhr.status);
40+
};
41+
xhr.onerror = reject;
42+
xhr.send(null);
43+
});
4444
}
4545
#elif ASSERTIONS
4646
assert(!isFileURI(url), "readAsync does not work with file:// URLs");
4747
#endif
48-
fetch(url, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}})
49-
.then((response) => {
50-
if (response.ok) {
51-
return response.arrayBuffer();
52-
}
53-
return Promise.reject(new Error(response.status + ' : ' + response.url));
54-
})
55-
.then(onload, onerror)
48+
return fetch(url, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}})
49+
.then((response) => {
50+
if (response.ok) {
51+
return response.arrayBuffer();
52+
}
53+
return Promise.reject(new Error(response.status + ' : ' + response.url));
54+
})
5655
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9817
1+
9804
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24102
1+
24088
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9801
1+
9788

0 commit comments

Comments
 (0)