Permalink
Browse files

react-native: BundleSegments: remove async/await

Reviewed By: fromcelticpark

Differential Revision: D6508646

fbshipit-source-id: 9e5c05bde5420cb030912eb187f6333af4b580f9
  • Loading branch information...
jeanlauliac authored and facebook-github-bot committed Dec 7, 2017
1 parent 9f33fe2 commit 1988ba1d7967dca04376e7e631e8910e5e79a6a7
Showing with 29 additions and 25 deletions.
  1. +29 −25 Libraries/Utilities/BundleSegments.js
@@ -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.