Aggregate calls using bluebird#7
Conversation
add all results and return
e64e576 to
8c299b4
Compare
| "posttest": "npm run lint && nsp check" | ||
| }, | ||
| "dependencies": { | ||
| "bluebird": "latest", |
There was a problem hiding this comment.
Please do not depend on latest. Always use ^x.y.z, "bluebird": "^3.5.0" in this particular case.
If you depend on latest and a new major version of your dependency is released, your application is very likely to break, because a new major version typically has backwards-incompatible changes.
| 'use strict'; | ||
|
|
||
| const app = require('../server'); | ||
| const promise = require('bluebird'); |
There was a problem hiding this comment.
We are usually using Promise as the variable name.
const Promise = require('bluebird');| function findTransaction(input) { | ||
| let find = transactionService.getFunction('Transaction', 'findById'); | ||
| return new Promise(function (resolve, reject) { | ||
| find(input, function(err, data) { |
There was a problem hiding this comment.
I think this is sort of an anti-pattern in Bluebird, use promisify instead - see http://bluebirdjs.com/docs/api/promise.promisify.html
|
|
||
| function getFunction(model, method) { | ||
| let functionName = model + '_' + method; | ||
| return this.createModel(model, {})[functionName]; |
There was a problem hiding this comment.
IIUC, this is the single place where you are looking up callback-based functions. In which case this is the place where you can promisify the function before you return it back.
return Promise.promisify(this.createModel(model, {})[functionName]);However, this will not work for functions that expect this to be set. Here is a better solution:
function getFunction(modelName, methodName) {
const functionName = modelName + '_' + methodName;
const model = this.createModel(modelName, {});
const method = Promise.promisify(model[functionName]);
return method.bind(model);
}There was a problem hiding this comment.
FWIW, this should not be needed once strongloop/loopback-connector-swagger#25 is landed and released.
add all results and return