Comments (8)
I'm trying to fork/patch right now... will submit a PR if I figure it out
from node-fetch.
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.
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.
Specs:
-
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.
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:
Promises/A+ 2.2.7.2 https://promisesaplus.com/#point-42
ECMAScript 2015:
25.4.1.3.2
http://www.ecma-international.org/ecma-262/6.0/#sec-promise-resolve-functions
Promise Resolve FunctionsLet then be Get(resolution, "then").
If then is an abrupt completion, then
a. Return RejectPromise(promise, then.[[value]]).25.4.2.1
http://www.ecma-international.org/ecma-262/6.0/#sec-promisereactionjob
PromiseReactionJob ( reaction, argument )Else, let handlerResult be Call(handler, undefined, «
argument»).If handlerResult is an abrupt completion, then
a. Let status be Call(promiseCapability.[[Reject]], undefined,
«handlerResult.[[value]]»).
b. NextJob Completion(status).—
Reply to this email directly or view it on GitHub
#59 (comment).
from node-fetch.
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.
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.
See https://github.com/niftylettuce/frisbee/blob/master/src/frisbee.js#L108-L114 for my implementation
from node-fetch.
Related Issues (20)
- Suggest: Align fetch function's type with the latest Typescript type on fetch function?
- node v20, closing server and re-listening between requests causes ECONNRESET HOT 3
- SSL error when setting host: header correctly HOT 1
- Node-fetch allows setting forbidden headers HOT 3
- Could not catch Premature close error HOT 1
- Broken clone() method in newer version of 3.3.* (at least)
- Critical dependency:
- failing without clear reason HOT 3
- FormData with invalid Content-Length HOT 2
- Pure ems bundle prevents node-fetch usage in libraries/frameworks HOT 2
- Can't import HOT 1
- v2.7.0: punycode is deprecated in 'whatwg-url' and 'tr46' HOT 3
- Faulty NPM upload 2.7.0 HOT 3
- The 'body' stream never ends for empty HTTP response bodies on Node 16 HOT 1
- Unhandled 'error' event when using AbortSignal to cancel requests HOT 2
- Andoka now
- bug issue
- Error [ERR_REQUIRE_ESM]: require() of ES Module
- Error [ERR_REQUIRE_ESM]: require() of ES Module HOT 1
- Not able to make network call with empty body
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-fetch.