senorcris/opengraphjs

View on GitHub
lib/utils.js

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * Source https://github.com/strongloop/loopback-connector-rest/blob/master/lib/utils.js#L3
 */
exports.createPromiseCallback = createPromiseCallback;

function createPromiseCallback() {
  var cb;

  if (!global.Promise) {
    cb = function(){};
    cb.promise = {};
    Object.defineProperty(cb.promise, 'then', { get: throwPromiseNotDefined });
    Object.defineProperty(cb.promise, 'catch', { get: throwPromiseNotDefined });
    return cb;
  }

  var promise = new Promise(function (resolve, reject) {
    cb = function (err, data) {
      if (err) return reject(err);
      return resolve(data);
    };
  });
  cb.promise = promise;
  return cb;
}

function throwPromiseNotDefined() {
  throw new Error(
    'Your Node runtime does support ES6 Promises. ' +
    'Set "global.Promise" to your preferred implementation of promises.');
}