[Vuejs]-What does double arrow function mean in Javascript?

3👍

This has nothing to do with arrow functions. It has to do with the then method on promises.

With a promise’s then method, the first argument is a fulfillment handleer and the second is a rejection handler. If the promise is fulfilled, the first is called with the fulfillment value. If the promise is rejected, the second is called with the rejection reason. Only one or the other (if either) will be called, never both (for the same promise).

Here’s a slightly edited version of the function partially shown in the qusetion, with some names changed and the return true; removed (because I couldn’t figure out where it was meant to be):

async check({ commit }) {
    await axios.get('check')
    .then(
        (value) => {                       // ***
            // Use the fulfillment value   // *** fulfillment handler
        },                                 // ***

        (reason) => {                      // ***
            // Deal with the rejection     // *** rejection handler
        }                                  // ***
    );
});

See MDN or the Promises A+ specification that JavaScript’s promises are based on.


It’s probably worth noting that there’s no particular reason to make this specific function an async function unless the goal is specifically to hide any fulfillment value the promise may have (or the rejection handler may supply). It does that, though, so perhaps that’s the purpose.

2👍

The Promises/A+ standard says that then can take two arguments:

promise.then(onFulfilled, onRejected)

The second one is the onRejected handler.

You don’t see this as often since catch() exists, but it’s still part of the standard.

Leave a comment