loopback/lib/utils.js

29 lines
749 B
JavaScript
Raw Normal View History

2015-07-01 11:43:25 +00:00
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 global.Promise(function(resolve, reject) {
2015-07-01 11:43:25 +00:00
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.');
}