1

First .then method is using arrow function syntax and the second .then method is using the function keyword syntax. Is this just inconsistent coding style or does it have a purpose?

store
      .dispatch(Actions.LOGIN, values)
      .then(() => {
        Swal.fire({
          text: "Sucess!",
          icon: "success",
          buttonsStyling: false,
          confirmButtonText: "Ok!",
          customClass: {
            confirmButton: "btn fw-bold btn-light-primary",
          },
        }).then(function () {
          // Go to page after successfully login
          router.push({ name: "dashboard" });
        });
      })
1
  • 2
    I don't think there is a purpose here, except for "just because", or "I forgot arrow functions exist for a sec" Commented Feb 25, 2022 at 15:37

2 Answers 2

1

I think that this is just inconsistent coding style, the end result is virtually the same (although in some instances you might be unable to use arrow functions) so it doesn't really matter.

Some developers like to use arrow functions, others like to use normal functions. Sometimes you might forget to use an arrow function, or are copying someone else's code and forget to reformat it to your own coding style.

If this bugs you out, you can use ESLint to enforce a consistent style for your project.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! The code was "professionally" written so I just wanted to confirm there was not some obscure quirk in JS with chaining/nesting .then methods. The project is using ESLint, but it must not be configured to scrutinize function syntax.
The result is the same, but only in this case because this isn't used in then callback. The opposite is common, and such questions end up as dupes of this one, stackoverflow.com/questions/20279484/…
@DaveHarig Besides function, there are nested thens, which is an antipattern and can cause issues. Professional or not, there's a place for improvements in this snippet, so your concerns are valid.
@EstusFlask I wondered about the nesting. I'll deep dive that next. Thx again!
0

No, it is not just inconsistency here you can read more.

And some cases you can not use arrow functions.

Also, without a webpack etc or some kind of packager it may not work for all browsers.

2 Comments

It's inconsistency in this case.
Thank you both for helping a noob. Inconsistency in this case and I took mental note for consideration of .this keyword treatment based on function type used. And for now I am just going to rely on Webpack/Polyfill magic to always save me from older browsers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.