How can I make Promise.reject (res.text ()) return text instead of [object promise]?

  • 0
    With this code
    .then (res = & gt; res.ok? Res.json (): Promise.reject (res.text ()))

    [object promise] is returned to return the error text, I have to do this:
    .then(res => res.ok ? res.json() : res.text())
    .then(res => if (res !== undefined) return Promise.reject(res))
    .catch(e => `Ошибка: ${e}`);

    tell me, can't you do it differently?
    And another question:
    .then(res => res.json())
    .then(res => res.users)

    why not return users immediately, like this?
    .then (res = & gt; res.json (). users)
    JavaScript Anonymous, Feb 10, 2019

  • 3 Answers
  • 0
    With this code

    .then (res = & gt; res.ok? res.json (): Promise.reject (res.text ()))





    the answer gave you in the previous question along with an example



    And another question:

    .then (res = & gt; res.json ())

    .then (res = & gt; res.users)



    why can't we return users immediately, like this?

    .then (res = & gt; res.json (). users)




    Because this is the specification of the method.

    Body.json ()

    Takes a Response stream and reads it to completion. It returns a promise that resolves with the result of parsing the body text as JSON, which is a JavaScript value of datatype object, string, etc.


    https://developer.mozilla.org/en/docs/Web/API / Body / json
    Levi Morales

  • 0
    No way. Promise.reject - Returns a promise .
    Felix Christensen

  • 0
    Rewrite your code using async / await and then instead of promises you can get the result immediately.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!