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;
|
|
|
|
}
|
|
|
|
|
2015-08-04 09:00:18 +00:00
|
|
|
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.');
|
|
|
|
}
|