function loadSegment(segmentId: number): Promise<void> {
  return Promise.resolve().then(() => {
    if (segmentId === 0) {
      return;
    }
    if (typeof global.__BUNDLE_DIGEST__ !== 'string') {
      throw IncorrectBundleSetupError();
    }
    const globalDigest = global.__BUNDLE_DIGEST__;
    let segmentLoader = segmentLoaders.get(segmentId);
    if (segmentLoader != null) {
      return segmentLoader;
    }
    const {fetchSegment} = global;
    if (fetchSegment == null) {
      throw new FetchSegmentNotAvailableError();
    }
    segmentLoader = new Promise((resolve, reject) => {
      fetchSegment(segmentId, error => {
        if (error != null) {
          reject(error);
          return;
        }
        resolve();
      });
    }).then(() => {
      const metaModuleId = (require: $FlowFixMe).packModuleId({
        segmentId,
        localId: 0,
      });
      const metaModule = dynamicRequire(metaModuleId);
      const digest: string =
        typeof metaModule === 'object' && metaModule != null
          ? (metaModule.BUNDLE_DIGEST: $FlowFixMe)
          : 'undefined';
      if (digest !== globalDigest) {
        throw new IncompatibleSegmentError(globalDigest, digest);
      }
    });
    segmentLoaders.set(segmentId, segmentLoader);
    return segmentLoader;
  });
}
Ensure that a bundle segment is ready for use, for example requiring some of
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.

Segment #0 is the main segment, that is always available by definition, so
we never try to load anything.

We don't use async/await syntax to avoid depending on `regeneratorRuntime`.