-1

So basically can anyone briefly go through with me what's the difference between

user.login(username,password,function(){
    //do something here
});

and

user.login(username,password).then(function(){
    //do something here
});

Which one should be in use and what's the benefits? I thought this topic regarding to javascript promises and callback function.

Thanks!

Peter.Wang
  • 2,051
  • 1
  • 19
  • 13

2 Answers2

0

It depends upon how user.login() is designed to work. If it is designed such that the third argument is a callback function that will be called when the login is complete, you would use the first form (and you would probably have to check an err argument sent to the callback).

If user.login() is designed to return a promise that will be fulfilled when the login is complete, then you would use the second form (and you probably want to add an error handler callback).

If it is designed to have both behaviors (always return a promise and call a callback if it is passed), then you can select either method.


If user.login() is designed such that either will work, then the second structure with the promise is much more extensible because you can take advantage of many of the advantages of promises:

  1. Easier to chain/sequence multiple async operations.
  2. Throw-safe in the callback
  3. Easier to coordinate this async operation with other async operations using things like Promise.all().
  4. More robust error handling - errors will propagate up in promises, but not in callbacks.

If you've ever written code with a whole bunch of async operations in it, some of which are sequential, some of which are parallel and some of which have to be coordinated with some of the others and this code needs to have robust error handling (such as in a server environment), you will very, very quickly find that promises make it many times easier to accomplish your goal. And, once you learn them, you will find they are even preferred for the simple situations too.

If you've ever written code where you need to carry out a bunch of async operations and you need to operate on all the returned data when all the async operations are done, promises make this trivial to code. Without promises, you have to write a lot of scaffolding code yourself to keep track of the data and keep track of when everything is done and somehow propagate errors if any occur.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

If what you're asking is "Which design is better?" (Assuming login() hasn't been implemented) Promises are an overall more extensible architecture.

If someone were coding the first version of the function, they'd have to make sure that the callback is not null. Additionally, it's harder to make changes to the called-back value; and harder to catch exceptions if the request fails. (Not many people tend to add an "onError" callback to their function, but that's made much easier if you're returning a promise object) Generally, if both are available, I would leave promises in promise form when possible.

Katana314
  • 8,429
  • 2
  • 28
  • 36