function _resolveAndLoad(req, res, id, loadOptions, modelConfig) {

  return new Promise((resolve, reject) => {

    if (isUUID.v4(id)) {
      // If we have a slug for this UUID, we redirect to it,
      // otherwise we stay on the UUID version of the URL
      modelConfig.DocumentModel
        .getWithData(id, loadOptions)
        .then(document => {
          if (document.canonicalSlugName) {
            _redirectToCanonical(req, res, id, modelConfig.basePath, document.canonicalSlugName);
            let e = new Error();
            e.name = 'RedirectedError';
            reject(e);
          } else
            resolve(document);
        })
        .catch(reject); // ID not found or other error

    } else {
      // We'll assume that the provided ID refers to a slug
      modelConfig.SlugModel
        .get(id)
        .then(slug => {
          modelConfig.DocumentModel
            .getWithData(slug[modelConfig.slugForeignKey], loadOptions)
            .then(document => {
              if (document.canonicalSlugName === slug.name)
                resolve(document);
              // We always want to redirect to the canonical name
              else {
                _redirectToCanonical(req, res, id, modelConfig.basePath, document.canonicalSlugName);
                let e = new Error();
                e.name = 'RedirectedError';
                reject(e);
              }
            })
            .catch(reject);
        })
        .catch(reject); // Slug not found or other error
    }
  });

}
//   basePath: base URL of any canonical URL we redirect to
base URL of any canonical URL we redirect to
base URL of any canonical URL we redirect to
function _resolveAndLoad(req, res, id, loadOptions, modelConfig) {
  return new Promise((resolve, reject) => {
    if (isUUID.v4(id)) {
      modelConfig.DocumentModel
        .getWithData(id, loadOptions)
        .then(document => {
          if (document.canonicalSlugName) {
            _redirectToCanonical(req, res, id, modelConfig.basePath, document.canonicalSlugName);
            let e = new Error();
            e.name = 'RedirectedError';
            reject(e);
          } else
            resolve(document);
        })
        .catch(reject); 
    } else {
      modelConfig.SlugModel
        .get(id)
        .then(slug => {
          modelConfig.DocumentModel
            .getWithData(slug[modelConfig.slugForeignKey], loadOptions)
            .then(document => {
              if (document.canonicalSlugName === slug.name)
                resolve(document);
              else {
                _redirectToCanonical(req, res, id, modelConfig.basePath, document.canonicalSlugName);
                let e = new Error();
                e.name = 'RedirectedError';
                reject(e);
              }
            })
            .catch(reject);
        })
        .catch(reject); 
    }
  });
}
function _resolveAndLoad ( req , res , id , loadOptions , modelConfig ) { return new Promise ( ( resolve , reject ) => { if ( isUUID . v4 ( id ) ) { modelConfig . DocumentModel . getWithData ( id , loadOptions ) . then ( document => { if ( document . canonicalSlugName ) { _redirectToCanonical ( req , res , id , modelConfig . basePath , document . canonicalSlugName ) ; let e = new Error ( ) ; e . name = 'RedirectedError' ; reject ( e ) ; } else resolve ( document ) ; } ) . catch ( reject ) ; } else { modelConfig . SlugModel . get ( id ) . then ( slug => { modelConfig . DocumentModel . getWithData ( slug [ modelConfig . slugForeignKey ] , loadOptions ) . then ( document => { if ( document . canonicalSlugName === slug . name ) resolve ( document ) ; else { _redirectToCanonical ( req , res , id , modelConfig . basePath , document . canonicalSlugName ) ; let e = new Error ( ) ; e . name = 'RedirectedError' ; reject ( e ) ; } } ) . catch ( reject ) ; } ) . catch ( reject ) ; } } ) ; }
basePath: base URL of any canonical URL we redirect to
basePath : base URL of any canonical URL we redirect to
// If we have a slug for this UUID, we redirect to it, // otherwise we stay on the UUID version of the URL // ID not found or other error // We'll assume that the provided ID refers to a slug // We always want to redirect to the canonical name // Slug not found or other error