react-native: BundleSegments: remove async/await

Reviewed By: fromcelticpark

Differential Revision: D6508646

fbshipit-source-id: 9e5c05bde5420cb030912eb187f6333af4b580f9
This commit is contained in:
Jean Lauliac 2017-12-07 07:13:40 -08:00 committed by Facebook Github Bot
parent 9f33fe2583
commit 1988ba1d79
1 changed files with 29 additions and 25 deletions

View File

@ -20,35 +20,39 @@ let segmentLoaders = new Map();
* its module. We cache load promises so as to avoid calling `fetchSegment` twice
* for the same bundle. We assume that once a segment is fetched/loaded, it is
* never gettting removed during this instance of the JavaScript VM.
*
* We don't use async/await syntax to avoid depending on `regeneratorRuntime`.
*/
async function loadForModule(moduleID: number): Promise<void> {
const {segmentId} = (require: $FlowFixMe).unpackModuleId(moduleID);
if (segmentId === 0) {
return;
}
let segmentLoader = segmentLoaders.get(segmentId);
if (segmentLoader != null) {
return await segmentLoader;
}
function loadForModule(moduleID: number): Promise<void> {
return Promise.resolve().then(() => {
const {segmentId} = (require: $FlowFixMe).unpackModuleId(moduleID);
if (segmentId === 0) {
return;
}
let segmentLoader = segmentLoaders.get(segmentId);
if (segmentLoader != null) {
return segmentLoader;
}
const {fetchSegment} = global;
if (fetchSegment == null) {
throw new Error(
'When bundle splitting is enabled, the `global.fetchSegment` function ' +
'must be provided to be able to load particular bundle segments.',
);
}
segmentLoader = new Promise((resolve, reject) => {
fetchSegment(segmentId, error => {
if (error != null) {
reject(error);
return;
}
resolve();
const {fetchSegment} = global;
if (fetchSegment == null) {
throw new Error(
'When bundle splitting is enabled, the `global.fetchSegment` function ' +
'must be provided to be able to load particular bundle segments.',
);
}
segmentLoader = new Promise((resolve, reject) => {
fetchSegment(segmentId, error => {
if (error != null) {
reject(error);
return;
}
resolve();
});
});
segmentLoaders.set(segmentId, segmentLoader);
return segmentLoader;
});
segmentLoaders.set(segmentId, segmentLoader);
return await segmentLoader;
}
module.exports = {loadForModule};