-1

How can i use this method from my own project/design

Github.prototype.handleErr = function(res) {
  if(!res.ok) {
   throw new Error('Something went wrong!!' + res.status);
  }

  return res;
 }

Inside another method but the same object

const git = new Github();

Github.prototype.getUser = (user) => {
return new Promise((resolve, reject) => {
    // Profile
    fetch(something)
    .then(this.handleErr)
    .then(response => response.json())
    .then(data => resolve(data))
    .catch(err => reject(err));
})

}

it didn't work with this.handleErr

i wanted to make a fetch request and use the git.handleErr() function inside .then() consumer, before handling the data response.

Grecdev
  • 899
  • 9
  • 14
  • 1
    What have you tried and what is the exact issue you're encountering? It's not really clear what you expect this code to do or why your expectations were not met. I think more context would really help clarify what it is you are asking. – Bash Jun 15 '19 at 20:29
  • I edited now, hope is clear enough. – Grecdev Jun 16 '19 at 09:06
  • 1
    Possible duplicate of [ES6 arrow functions not working on the prototype?](https://stackoverflow.com/questions/31755186/es6-arrow-functions-not-working-on-the-prototype) – str Jun 16 '19 at 09:10
  • Look again i added the whole script – Grecdev Jun 16 '19 at 09:16

1 Answers1

0

Ok so i figured out. I modified the arrow function syntax

Github.prototype.getUser = (user) => {}

to the normal syntax

Github.prototype.getUser = function(user) {}

Acording to this it was referred as a global/undefined this. so that's why it didn't work in the first instance. Thank you str.

Grecdev
  • 899
  • 9
  • 14