skip to Main Content

Is there a way to fix the format with promises in VS Code?
when I format with prettier or any other options, the code look like this /

const GET_USERS = () => {
  fetch('https://jsonplaceholder.typicode.com/todos/1').then((resolve) => {
    resolve.json().then((json) => {
      console.log(json);
    });
  });
};

and I really want that the code look like this /, without stop using prettier

const GET_USERS = () => {
  fetch('https://jsonplaceholder.typicode.com/todos/1')
    .then((resolve) => {resolve.json()
    .then((json) => {console.log(json);
    });
  });
};

2

Answers


  1. Try with this code:

    const GET_USERS = () => {
      fetch("https://jsonplaceholder.typicode.com/todos/1")
        .then((resolve) => resolve.json())
        .then((json) => {
          console.log(json);
        });
       // catch errors here
    };
    

    Check how I’m chaining promises, one after another. Don’t forget to use a catch for errors. Also, maybe you are trying to return something from that function?

    Login or Signup to reply.
  2. You can try like this:

    // prettier-ignore
    const GET_USERS = () => {
      fetch('https://jsonplaceholder.typicode.com/todos/1')
        .then((resolve) => {resolve.json()
        .then((json) => {console.log(json);
        });
      });
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search