Coder Social home page Coder Social logo

Comments (8)

niftylettuce avatar niftylettuce commented on May 19, 2024

I'm trying to fork/patch right now... will submit a PR if I figure it out

from node-fetch.

niftylettuce avatar niftylettuce commented on May 19, 2024

Before:

/**
 * Decode response as json
 *
 * @return  Promise
 */
Response.prototype.json = function() {

    return this._decode().then(function(text) {
        return JSON.parse(text);
    });

}

After:

/**
 * Decode response as json
 *
 * @return  Promise
 */
Response.prototype.json = function() {

    return this._decode().then(function(text) {
    try {
      var body = text && JSON.parse(text);
    } catch (e) {
      var err = e;
    } finally {
      if (err)
        return Response.Promise.reject(err);
      return body;
    }
    });

}

from node-fetch.

TimothyGu avatar TimothyGu commented on May 19, 2024

With a proper Promises/A+ (or ES2015, which is based on the Promises/A+ spec) implementation (like the one used by this module), this is not needed. In fact, this pattern is exactly the kind Promises are trying to prevent.

Do you have a specific test case where the method is not working properly?

from node-fetch.

TimothyGu avatar TimothyGu commented on May 19, 2024

Specs:

  • Promises/A+ 2.2.7.2

  • ECMAScript 2015:

    25.4.1.3.2 Promise Resolve Functions
    8. Let then be Get(resolution, "then").
    9. If then is an abrupt completion, then
      a. Return RejectPromise(promise, then.[[value]]).

    25.4.2.1 PromiseReactionJob ( reaction, argument )
    6. Else, let handlerResult be Call(handler, undefined, «argument»).
    7. If handlerResult is an abrupt completion, then
      a. Let status be Call(promiseCapability.[[Reject]], undefined, «handlerResult.[[value]]»).
      b. NextJob Completion(status).

from node-fetch.

niftylettuce avatar niftylettuce commented on May 19, 2024

doesn't that spec say if abrupt completion then use reject promise? isn't
throwing an error an abrupt completion?
On Nov 26, 2015 6:55 PM, "Timothy Gu" [email protected] wrote:

Specs:


Reply to this email directly or view it on GitHub
#59 (comment).

from node-fetch.

TimothyGu avatar TimothyGu commented on May 19, 2024

You are correct. But isn't "rejected promise" exactly what you are trying to do? At least in your implementation you are returning a rejected promise in the onFulfilled callback.

Or am I missing something?

from node-fetch.

bitinn avatar bitinn commented on May 19, 2024

I don't see why we should catch JSON parser error manually, you should be able to handle it in catch clause as you handle other type of network errors. Maybe an example might help to explain your problem.

from node-fetch.

niftylettuce avatar niftylettuce commented on May 19, 2024

See https://github.com/niftylettuce/frisbee/blob/master/src/frisbee.js#L108-L114 for my implementation

from node-fetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.