Skip to content

Promises for operation hooks #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2015
Merged

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Feb 17, 2015

  • Allow the observer functions passed to ModelBaseClass.observe to return a promise instead of calling the callback.
  • Support both promise and callback styles in ModelBaseClass.notifyObserversOf.

Related: strongloop/strong-remoting#179 and #447
Connect strongloop/loopback#418

/to @raymondfeng Please review.
/cc @partap This patch provides the infrastructure needed for promise support, it's based on my comments in your PR. My plan is to land this PR first, so that you can use this infrastructure in your PR.

Allow the observer functions passed to `ModelBaseClass.observe`
to return a promise instead of calling the callback.
@bajtos bajtos added the #review label Feb 17, 2015
@coodoo coodoo mentioned this pull request Feb 17, 2015
32 tasks
@raymondfeng
Copy link
Contributor

LGTM

Support both promise and callback styles in
ModelBaseClass.notifyObserversOf.

When there is no callback supplied, the method returns a promise that
is resolved (or rejected) with the result.
@bajtos bajtos force-pushed the feature/promises-for-operation-hooks branch from 9538dd5 to 76ebdcb Compare February 17, 2015 17:19
bajtos added a commit that referenced this pull request Feb 17, 2015
@bajtos bajtos merged commit 9e0a221 into master Feb 17, 2015
@altsang altsang removed the #review label Feb 17, 2015
@bajtos bajtos deleted the feature/promises-for-operation-hooks branch February 17, 2015 17:23
@bajtos bajtos mentioned this pull request Feb 17, 2015
@ritch
Copy link
Contributor

ritch commented Feb 19, 2015

@bajtos are you updating the docs / examples (atleast creating examples) for this?

@bajtos
Copy link
Member Author

bajtos commented Feb 20, 2015

@ritch Good catch, I added a task to strongloop/loopback#418 to update docs & examples once we have all pieces in place.

@ctcampb3
Copy link

ctcampb3 commented Nov 21, 2017

@bajtos, I see that Using promises says you can use promises with Operation hooks. But I'm missing where the example is. It took me a few days to figure out that you don't need to return next() in the promise chain. Is there any way we could have an example out there so others don't go through the same struggle? Returning next caused the Operation hooks to be called twice.

@bajtos
Copy link
Member Author

bajtos commented Dec 8, 2017

@ctcampb3 I am sorry to hear that you lost so much time trying to figure this out :( Perhaps the following example would be a good one to update?
https://github.com/strongloop/loopback-example-app-logic#define-a-model-operation-hook

BTW I think we have a similar problem in remoting hooks too, see strongloop/strong-remoting#406 (comment) and strongloop/strong-remoting#406 (comment). It may be possible to improve our code to detect the situation where next was called and a promise was returned too and warn the user.

@ctcampb3
Copy link

ctcampb3 commented Dec 8, 2017

@bajtos , I think those issues look pretty similar. For the example I thought it might be good to have it on the Using promises page under the LoopBack support for promises section. Having an example with a warning here would make it more clear.
Something generic like:

When returning a Promise instead of a callback the callback function should not be called at all. For example:

MyModel.observe('before save', function filterProperties(ctx, next) {
  // Resolving with a callback
  next();
});
MyModel.observe('before save', function filterProperties(ctx, next) {
  // Resolving with a promise
  return Promise.resolve();
});

@bajtos
Copy link
Member Author

bajtos commented Dec 12, 2017

@ctcampb3 makes sense. Would you mind contributing this doc improvement yourself? See this page to get you started: http://loopback.io/doc/en/contrib/doc-contrib.html Please mention my handle in the pull request to make sure I notice it.

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.

5 participants