From 853ca0349166af0d4cd82de21db197aa57eab7eb Mon Sep 17 00:00:00 2001 From: Jue Hou Date: Mon, 14 Dec 2015 17:16:20 -0500 Subject: [PATCH] Use bluebird in utils.js Replace `global.Promise` with `bluebird` --- 3.0-RELEASE-NOTES.md | 14 ++++++++++++++ lib/scope.js | 2 +- lib/utils.js | 16 +--------------- package.json | 2 +- test/async-observer.test.js | 1 + test/init.js | 4 ---- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/3.0-RELEASE-NOTES.md b/3.0-RELEASE-NOTES.md index 3fc7879d..c8a5ed89 100644 --- a/3.0-RELEASE-NOTES.md +++ b/3.0-RELEASE-NOTES.md @@ -5,3 +5,17 @@ always describe the impact on users and instructions for upgrading applications from 2.x to 3.0. See also https://github.com/strongloop/loopback/blob/master/3.0-DEVELOPING.md + +## Always use bluebird as promise library + +In version 3.0, we always use bluebird as our promise library +instead of `global.Promise`. +We consider Bluebird API a part of LoopBack API from now on, +you are welcome to use any Bluebird-specific methods in your applications. + +If you are using LoopBack with a custom promise implementation provided +via `global.Promise`, +you will have to check all places where you are using non-standard promise API +and update them to use Bluebird API instead. + +Please see [Related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/790) here. \ No newline at end of file diff --git a/lib/scope.js b/lib/scope.js index f84d4bee..878f9890 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -407,7 +407,7 @@ function defineScope(cls, targetClass, name, params, methods, options) { } } } - + options = options || {}; filter = filter || {}; var targetModel = definition.targetModel(this._receiver); diff --git a/lib/utils.js b/lib/utils.js index 15852f1b..fa387d9a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -19,6 +19,7 @@ exports.findIndexOf = findIndexOf; var traverse = require('traverse'); var assert = require('assert'); +var Promise = require('bluebird'); function safeRequire(module) { try { @@ -478,15 +479,6 @@ function sortObjectsByIds(idName, ids, objects, strict) { 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); @@ -497,12 +489,6 @@ function createPromiseCallback() { return cb; } -function throwPromiseNotDefined() { - throw new Error( - 'Your Node runtime does support ES6 Promises. ' + - 'Set "global.Promise" to your preferred implementation of promises.'); -} - /** * Dedupe an array * @param {Array} an array diff --git a/package.json b/package.json index 88fb22b3..1dec2b9c 100644 --- a/package.json +++ b/package.json @@ -32,12 +32,12 @@ "node >= 0.6" ], "devDependencies": { - "bluebird": "^2.9.9", "mocha": "^2.1.0", "should": "^8.0.2" }, "dependencies": { "async": "~1.0.0", + "bluebird": "^3.1.1", "debug": "^2.1.1", "depd": "^1.0.0", "inflection": "^1.6.0", diff --git a/test/async-observer.test.js b/test/async-observer.test.js index f840be14..6938733a 100644 --- a/test/async-observer.test.js +++ b/test/async-observer.test.js @@ -1,5 +1,6 @@ var ModelBuilder = require('../').ModelBuilder; var should = require('./init'); +var Promise = require('bluebird'); describe('async observer', function() { var TestModel; diff --git a/test/init.js b/test/init.js index 0c63d304..207018f0 100644 --- a/test/init.js +++ b/test/init.js @@ -26,7 +26,3 @@ if (!('getModelBuilder' in global)) { return new ModelBuilder(); }; } - -if (!('Promise' in global)) { - global.Promise = require('bluebird'); -}