Skip to content
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

alterItems now works with an async callback function #371

Merged

Conversation

sean-nicholas
Copy link
Contributor

@sean-nicholas sean-nicholas commented Mar 10, 2018

Summary

alterItems did not work with a async callback function. See #367
This PR fixes #367

Documentation at: feathers-plus/docs#19

@eddyystop
Copy link
Collaborator

eddyystop commented Mar 11, 2018

Thanks for the PR! There's a bit more to do.

Feathers is supported on NodeJS v6 and v8. v6 does not support async/await. So the actual hooks cannot use async/await anywhere.

Its preferable the tests also do not use async/await . Where that is awkward, some tests can be placed in folder tests-async and called from tests using a stub like fast-join-stub.test.js.

@sean-nicholas
Copy link
Contributor Author

Ah okay. I will implement the changes asap :)

@sean-nicholas
Copy link
Contributor Author

Hey, I updated the code and the tests and removed every async / await

@eddyystop eddyystop merged commit 1f2f95e into feathersjs-ecosystem:master Jun 5, 2018
@eddyystop
Copy link
Collaborator

Thank you.

@eddyystop
Copy link
Collaborator

In v4.12.0. Docs updated.

@bertho-zero
Copy link
Contributor

bertho-zero commented Jun 19, 2018

This breaks my workflow with synchronous alterItems, they all turn into promises, it's a breaking change.

I think if there is no asynchronous then we should not return a promise.

EDIT: Or write on the doc that this hook now returns a promise in any case ?

@sean-nicholas
Copy link
Contributor Author

Hmm, I think it would be cleaner to change the docs. Because different return values based on the callback might be confusing:

  • Callback func returns not a promise: Don't return a promise
  • Callback func returns a promise: Return a promise
  • Callback func returns a promise for some records: Return a promise

@eddyystop What do you think?

PS: How do you use alterItems? If you use it as normal hook it should simply work. Do you call it yourself in a custom hook?

@eddyystop
Copy link
Collaborator

eddyystop commented Jun 19, 2018

The hook may be called with a Promise as a param with the caller rightly expecting the hook to return a Promise. That Promise param can be resolved by the time the hook processes it. So I don't think we can use the existance of a Promise param to determine if a Promise result is returned or not.

Is there something else we can do other than having separate signatures/hooks for sync and async params?

This issue is pretty important as alterItems is likely used sync more often than async.

@sean-nicholas
Copy link
Contributor Author

@eddyystop Don't get what you're trying to say 😄
What about this solution: https://gist.github.com/sean-nicholas/e93068b08239eac55b7140e31fb4c9c4

Be careful. I hacked it together. It does not use the hook context and therefore does not work with feathers. I just ran it in a quokka env to test it and therefore simplified the in- & output.

It now behaves as I outlined in my other comment:

  • Callback func returns not a promise: Don't return a promise
  • Callback func returns a promise: Return a promise
  • Callback func returns a promise for some records: Return a promise

@bertho-zero
Copy link
Contributor

hasPromises is the most viable solution in my opinion. (I did not test the code)

@eddyystop
Copy link
Collaborator

eddyystop commented Jun 19, 2018

What you suggest should work. It was used at least in some old common hook code; maybe its still used.

I would suggest calculating hasPromises within the map loop.

I can fix the hook Wed or Thurs, else I can look at a PR.

@eddyystop
Copy link
Collaborator

@sean-nicholas, please check #409 works with your code.

@sean-nicholas
Copy link
Contributor Author

#409 works for me 👍

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

Successfully merging this pull request may close these issues.

Async functions in alterItems don't work
3 participants