SeyZ / jsonapi-serializer

A Node.js framework agnostic library for (de)serializing your data to JSON API
MIT License
735 stars 196 forks source link

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

Open moltar opened 7 years ago

moltar commented 7 years ago

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

SeyZ commented 7 years ago

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

moltar commented 7 years ago

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 commented 7 years ago

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 commented 6 years ago

@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 commented 5 years ago

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 commented 4 years ago

This should not be async.

tobilen commented 3 years ago

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