util.js 806 B

1234567891011121314151617181920212223
  1. export var createPromiseCallback = function () {
  2. var cb;
  3. if (!Promise) {
  4. cb = function () { };
  5. cb.promise = {};
  6. var throwPromiseNotDefined = function () {
  7. throw new Error('Your Node runtime does support ES6 Promises. ' +
  8. 'Set "global.Promise" to your preferred implementation of promises.');
  9. };
  10. Object.defineProperty(cb.promise, 'then', { get: throwPromiseNotDefined });
  11. Object.defineProperty(cb.promise, 'catch', { get: throwPromiseNotDefined });
  12. return cb;
  13. }
  14. var promise = new Promise(function (resolve, reject) {
  15. cb = function (err, data) {
  16. if (err)
  17. return reject(err);
  18. return resolve(data);
  19. };
  20. });
  21. cb.promise = promise;
  22. return cb;
  23. };