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

Why does Serializer returns a value, yet Deserializer returns a promise? #138

Open
moltar opened this issue May 4, 2017 · 7 comments · May be fixed by #258
Open

Why does Serializer returns a value, yet Deserializer returns a promise? #138

moltar opened this issue May 4, 2017 · 7 comments · May be fixed by #258

Comments

@moltar
Copy link

moltar commented May 4, 2017

It feels inconsistent. Any technical reason for that? I think ideally the entire interface should be promise based (and optionally callback based).

@SeyZ
Copy link
Owner

SeyZ commented May 4, 2017

Hey @moltar - Because Serializer is a synchronous process and Deserializer can be asynchronous

@moltar
Copy link
Author

moltar commented May 4, 2017

I can see cases where Serializer can also be async tho. E.g. when you need to pull up extra data from async sources for example. My point is that the interfaces should be similar. This really took me by surprise. The docs are not even mentioning this for Deserializer.

@SeyZ
Copy link
Owner

SeyZ commented May 4, 2017

I think serializer is never async. It's your job to retrieve the data (async or not) before passing the payload to the serializer.

Here's a quick note about promise on the README: https://github.com/SeyZ/jsonapi-serializer#notes-on-promises

I understand your point, but not sure it will be valuable to switch to async in the serializer if this is always sync just to be consistent.

@cubitworx
Copy link

@SeyZ Can you please explain why deserialize needs to be async? There is no code inside deserialize that requires async behaviour!? You are just assuming it should always be non-blocking & forcing that on the user. Surely if the user wanted async they could just wrap the call in a promise of their own?

Since you don't have a sync deserialize option I need to look to another library to avoid the overhead of making all my middleware async just for 1 library.

@bradrobertson
Copy link

I'm just stumbling on this also. I'd like to understand how deserializing is ever async? There's no I/O happening as part of this process so I don't really understand how there's any benefit to making this a Promise. Javascript is single threaded (for now at least) and if there's no I/O, there's no appreciable benefit to make this async from what I can tell.

Unless I'm missing something about automatically fetching documents through an API or something?? If you have the full payload of an object and you're converting its format, unless you had multiple threads parsing/combining and were coordinating things, I don't see any asynchronous behaviour here.

Am I missing something?

@kevingilbert100
Copy link

This should not be async.

@tobilen tobilen linked a pull request Aug 30, 2021 that will close this issue
@tobilen
Copy link

tobilen commented Aug 30, 2021

I created a PR to adress this. Meanwhile, feel free to use the deployed fork: https://www.npmjs.com/package/jsonapi-serializer-tobilen

PS.: This fork will be deprecated from npm if/when the PR is merged

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 a pull request may close this issue.

6 participants