0

I created the next function that should return the response, data and error in case if exists.

const login = function(cb) {
  fetch('https://jsonplaceholder.typicode.com/todos/1')
    .then((response) => {
      cb(null, null, response);
      return response.json();
    })
    .then((data) => {
      cb(data, null, null);
    })
    .catch((e) => {
      console.log('error inside catch:', e.message)
      cb(null, null, e)
    })
}

console.log(login((data, response, err) => console.log('data', data, 'response', response, 'error', err)))

So, I have to return all these values, but I can return just data. If I change this: https://jsonplaceholder.typicode.com/todos/1 to this: https://j123sonplaceholder.typicode.com/todos/1, to return err, I get undefined. The same issue is with response.
Question: How to get all these values?

4
  • I copied your Code to a jsfiddle and it logs 'error inside catch' when supplying a nonexistent url. If there is an error, there is no data. Commented Feb 28, 2021 at 12:21
  • @ccarstens how to get all these data in ` console.log('data', data,'response', response, 'error',err))`? Commented Feb 28, 2021 at 12:24
  • Your callback has three arguments but you call it always with just the first. hence, whatever you pass to the callback ends up as data. try cb(null, null, errorObject) to pass an error Commented Feb 28, 2021 at 12:33
  • @ccarstens, i changed the code above, but why i don't get the response in the console.log()? Commented Feb 28, 2021 at 12:41

1 Answer 1

3
// you write
cb(response);
cb(data);
cb(error);

// so the data and error below make no sense
cb(response, data, error)

You passed 3 params to the cb when using it in console.log. But in the login function declaration, cb accepts only 1 param.
That means your console.log always prints 2 times, the first is the Response of API call and the second is the data (if success - then) or error (if fail - catch).

const arr = []
const login = function (cb) {
    // const arr = []; move array inside login function if not use outside
    fetch("https://jsonplaceholder.typicode.com/todos/1")
      .then((response) => {
        arr[0] = response;
        return response.json();
      })
      .then((data) => {
        arr[1] = data;
      })
      .catch((e) => {
        arr[2] = e;
      })
      .finally(() => {
        cb(...arr);
      });
  };

login((response, data, err) => console.log('response', response, 'data', data, 'error', err))
// console.log(arr[0]) // response
// console.log(arr[1]) // data
// console.log(arr[2]) // error
Sign up to request clarification or add additional context in comments.

7 Comments

i changed the code above, but why i don't get the response in the console.log(). Why? ow to solve?
Don't do that, It will always run 2 times with unexpected behavior. Wait for me 1 min I'll add the code to my answer.
i got you, but how to return and data and response? I understand that i make 2 renders, but how to get both values? Thanks. Could you please add this feature in your anseer?
could you help?
but how to get data in console? For example i want to get data and response, how to do this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.