Skip to content

Aggregate calls using bluebird#7

Merged
deepakrkris merged 1 commit into
masterfrom
aggregateAccount
Mar 11, 2017
Merged

Aggregate calls using bluebird#7
deepakrkris merged 1 commit into
masterfrom
aggregateAccount

Conversation

@deepakrkris

Copy link
Copy Markdown
Collaborator

add all results and return

add all results and return
@deepakrkris
deepakrkris merged commit cc831a0 into master Mar 11, 2017
@deepakrkris
deepakrkris deleted the aggregateAccount branch March 11, 2017 06:48
"posttest": "npm run lint && nsp check"
},
"dependencies": {
"bluebird": "latest",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, this should not be needed once strongloop/loopback-connector-swagger#25 is landed and released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants