0
votes

I have a question about asynchronous/synchronous when we write code in JS and I have done google search but I am still a bit confused.

I understand that we use callback functions when we want to make sure that the callback function is executed only after the asynchronous task (such as accessing database) in the outer function is complete. I understand how deserialize and serialize user works.

I am confused why when we do serialize user or deserialize user with passport.js we need a callback function like this ?

passport.serializeUser((user, done) => {

done(null, user.id);

});

If all we want is that the inner arrow function that is passed as an argument to serializeUser() to be executed only after serializeUser() is finished. Or why do we need to pass it as a callback function instead of calling that arrow function below serializeUser() ? I thought JS is synchronous so it will execute the arrow function after serializeUser() is completed anyway ?

I only found serializeUser() documentation in passport documentation on how to use it, but not its implementation so I am also confused whether serializeUser() or deserializeUser()( or any other passport functions) are asynchronous functions ?

Thank you !

1
serializeUser will run the callback function whenever it want. You need to pass it as a function because that's how serializeUser is implemented. You cannot simply run the function "below" serializeUser because you need the arguments the callback function receives (like, say, the user). See stackoverflow.com/q/27637609/438992.Dave Newton
can can code it below of you promisize it and use async/awaitdandavis
@DaveNewton: Thank you. I wonder does this still mean that the callback function is only called after serializeUser() is finished ?bubble rain
@dandavis: Thank you. I wonder can you elaborate more how to use promise here ? I am also about the use of async/await. I wonder is serializeUser() not asynchronous then ? Because we need to use async/await.bubble rain
@bubblerain The callback is called whenever serializeUser calls it. It may be at the end of its normal flow, it may not be--all you could do is look at the source to see how/when the callback is used.Dave Newton

1 Answers

1
votes

enter image description here

It is a fragment of this function on github (https://github.com/jaredhanson/passport/blob/08f57c2e3086955f06f42d9ac7ad466d1f10019c/lib/authenticator.js).

As you can see this function takes a fn parameter. Then it checks if it is a function. If yes it pushes it to this._serializers. Later it does 'some magic' on it, I think it's not important now.

As you can read in jsdoc, serializeUser purpose is to register a function used to serialize user objects. So you pass some function which is invoked later in a code with 2 arguments which labels on that particular function level are user and done.

They left you some space which you can fill with your own code. You can tell how this will behave. They gave you an opportunity to implement your own logic.

Simple example:

function doSomeMagic(fn) {
  // I do some my staff
  const a = 5;
  const b = 10;

  // and now I let you decide what to do
  // you can implement your own logic in my function
  const result = fn(a, b);

  console.log(result);
}

doSomeMagic((a, b) => {
  return a * b;
});

doSomeMagic((a, b) => {
  return a + b;
});

//output:
// 50
// 15

That's exactly what they've done. You can take my function, I give you two arguments do whatever you want. You can multiply, add, substract, whatever you want. I don't have to write separated functions to do math, I can write one, pass values as arguments and let you decided what operation you want to do. And it doesn't mean my code is going to run in an async way.