0

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 !

bubble rain
  • 411
  • 1
  • 5
  • 10
  • 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 https://stackoverflow.com/q/27637609/438992. – Dave Newton May 21 '20 at 18:55
  • 1
    can can code it below of you promisize it and use async/await – dandavis May 21 '20 at 19:01
  • @DaveNewton: Thank you. I wonder does this still mean that the callback function is only called after serializeUser() is finished ? – bubble rain May 21 '20 at 19:54
  • @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 May 21 '20 at 19:56
  • @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 May 21 '20 at 20:04

1 Answers1

1

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.

Jacob Tobiasz
  • 428
  • 2
  • 13
  • Hi Jakub ! Thank you. I think I realize now that callbacks can be asynchronous and synchronous. So i think the purpose of passing a function to `serializeUser()` in this case is so that we can implement the function on our own, instead of the normal purpose of asynchronous callback ? – bubble rain May 21 '20 at 20:05
  • Yep. It's just a 'feature' to left blank space to let somebody else fill it with his own code. E.g. you can add there `console.log('hello');` and it will print it every execution. I think `doSomeMagic` example is pretty good to see why this may be powerful. – Jacob Tobiasz May 21 '20 at 20:07
  • Great ! Thank you – bubble rain May 21 '20 at 20:20