Skip to content

Commit

Permalink
react-native: BundleSegments: remove async/await
Browse files Browse the repository at this point in the history
Reviewed By: fromcelticpark

Differential Revision: D6508646

fbshipit-source-id: 9e5c05bde5420cb030912eb187f6333af4b580f9
  • Loading branch information
Jean Lauliac authored and facebook-github-bot committed Dec 7, 2017
1 parent 9f33fe2 commit 1988ba1
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions Libraries/Utilities/BundleSegments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};

0 comments on commit 1988ba1

Please sign in to comment.