News for hackers. Stuff that matters.
Re:VS Code can do that? by scottmf on Saturday April 21, 2018 @12:26PM (#16892016)
Slightly off topic but this code in example #5:

  get() {
    return new Promise((resolve, reject) => {
      fetch(`${baseAPI}/heroes`)
        .then(response => response.json())
        .then(json => resolve(json))
        .catch(err => {
          reject (err);
        });
    });
  }
Why not just:

  get() {
    return fetch(`${baseAPI}/heroes`)
      .then(response => response.json());
  }
Re:VS Code can do that? by AaronFriel on Saturday April 21, 2018 @01:35PM (#16892338)
That has fewer brackets to colorize.
Re:VS Code can do that? by akerro on Saturday April 21, 2018 @02:30PM (#16892603)
By the end of a day, he reports number of lines written and committed.
Re:VS Code can do that? by rofrol on Monday April 23, 2018 @11:34PM (#16909654)
agree. I would only add rejecting when !response.ok

  function fetchPost(uri, body) {
    return new Request(uri, {
      method: 'POST',
      mode: 'cors',
      headers: {
        'Authorization': 'token 123412341234'
      },
      body: JSON.stringify(body)
    })
  }
  
  function fetchStatus(response) {
    return response.ok ? Promise.resolve(response) : Promise.reject(new Error(response.statusText));
  }
  
  fetch('https://httpstat.us/500')
    .then(fetchStatus)
    .then(function(result) {
      console.log(result)
    })
    .catch(function(err) {
      console.log('BAD:', err)
    });
https://medium.com/@luminarious/i-was-trying-out-fetch-liter...
Re:VS Code can do that? by azerothian on Saturday April 21, 2018 @06:51PM (#16893798)
or

  async get() {
    const response = await fetch(`${baseAPI}/heroes`);
    return response.json();
  }
Re:VS Code can do that? by OutThisLife on Sunday April 22, 2018 @01:03AM (#16894792)
or

  async get () {
    return await (await fetch(`${baseAPI}/heroes`)).json()
  }
Re:VS Code can do that? by Boulth on Sunday April 22, 2018 @08:28AM (#16895878)
If you see `return await` glued together just like that you can drop the `await` as it's effectively a no-op in this case.
Re:VS Code can do that? by daybreaker2 on Saturday April 21, 2018 @02:41PM (#16892667)
Because he needs it wrapped in a promise, sending a resolve/reject?
Re:VS Code can do that? by glitch003 on Saturday April 21, 2018 @02:49PM (#16892707)
Fetch and response.json() both return promises. So it's still a promise.
Re:VS Code can do that? by robotpony on Saturday April 21, 2018 @11:17PM (#16894542)
A promise is a promise ...
Re:VS Code can do that? by Guillaume86 on Saturday April 21, 2018 @04:57PM (#16893397)
True and in this particular example for response.json() it doesn't even matter.