2016-05-04 00:10:46 +00:00
|
|
|
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
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.');
|
|
|
|
}
|