Still struggle to understand callbacks

<%= @topic_view.topic.title %>
<%= @topic_view.topic.average_rating %> <%= @topic_view.topic.posts.count { |p| !!p.custom_fields['rating'] } %>
passport.serializeUser((user, done) => {
      done(null, user._id);
});

The passport method ‘serializeUser()’ takes two arguments:

  • user
  • done - done never gets defined as a function?

serializeUser() does ‘something’ with user.
serializeUser() runs the done() function to return null and a serialised user._id

Similarly when we have a next() function
function (req, res, next):

next({ "message": "This is a message" })

what calls next?
and how is next outputting an object?

If I remember correctly (and it’s been a while since I did PP), serializeUser gets called by the inner mechanisms of PP itself, So both of those values will be passed in as parameters by PP and you don’t need to worry about them. You are just defining what you want serializeUser to do. Usually the default implementation works. But what if the unique user id was stored differently? Now you could use whatever you want.

Callbacks are weird. I would recommend taking a side trip. Maybe look up some youtube tutorials where you create your own functions that use callbacks - that’s what really cemented it for me. I wrote my own versions on map and findIndex and such and suddenly it all made sense.