Recently, I came across some JavaScript code examples using classes where async methods were declared without the function
keyword or arrow functions. Here is an example of such code:
export default class CartClient {
async getCart(authToken, cartId) {
const request = this.request
.url(`${this.url}/${cartId}`)
.get()
.auth(authToken)
.withNoCache()
.build();
const response = await fetch(request);
return await response.json();
}
}
This was new to me as I was used to creating functions using either the function
keyword or arrow functions like in the examples below:
async function getCart() {
// implementation
}
or
async getCart = () => {
// implementation
}
I would like to know why the function
keyword is not needed anymore when declaring methods. Is it a JavaScript feature or specific to React? I noticed this in a React project.