Hacker News .hnnew | past | comments | ask | show | jobs | submitlogin

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());
  }


That has fewer brackets to colorize.


By the end of a day, he reports number of lines written and committed.


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...


or

  async get() {
    const response = await fetch(`${baseAPI}/heroes`);
    return response.json();
  }


or

  async get () {
    return await (await fetch(`${baseAPI}/heroes`)).json()
  }


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.


Because he needs it wrapped in a promise, sending a resolve/reject?


Fetch and response.json() both return promises. So it's still a promise.


A promise is a promise ...


True and in this particular example for response.json() it doesn't even matter.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: