[fixed]The .then() function is not compatible with Promises
See original GitHub issueThe faux .then()
function added in 1.3 completely misses the point of promises and breaks even in simple cases.
request.get(someCorrectUrl)
.then(function(){return 1;})
.then(console.log.bind(console));
Should print 1
, but throws an unhandled exception.
request.get('fail').then(function(){});
TypeError: reject is not a function
request.get('fail')
.then(undefined, function(){throw Error();})
.catch(function(){})
.then(…).catch is not a function
Please don’t try to reimplement promises — just enable use of a correct library.
I suggest:
Request.prototype.then = function(resolve, reject) {
var self = this;
return new Promise(function(resolve, reject){
self.end(function(err, res){
if (err) reject(err); else resolve(res);
});
})
.then(resolve, reject);
};
The code above assumes that promises are either supported natively (already the case in majority of browsers and even Node 0.12) or polyfilled by the user (which is a common practice, e.g. ES6 transpilers do it).
Issue Analytics
- State:
- Created 8 years ago
- Comments:33 (17 by maintainers)
Top Results From Across the Web
[fixed]The .then() function is not compatible with Promises #722
The faux .then() function added in 1.3 completely misses the point of promises and breaks even in simple cases. request.get(someCorrectUrl) .
Read more >Promise.prototype.then() - JavaScript - MDN Web Docs
The then() method of a Promise object takes up to two arguments: callback functions for the fulfilled and rejected cases of the Promise....
Read more >node.js - Promise being resolved before the beginning of test
I tried increasing the timeout but it was no help as my code was incorrect. I assumed that the NeDB initialization function accepted...
Read more >Callback and Promise Support in your Node.js Modules
Return callbacks as well as promises from your functions to be both, backward-compatible and future proof! Update — December 20th 2017. We've ...
Read more >Why You Shouldn't Mix Promise.then() With Async/Await Syntax
Why You Shouldn't Mix Promise.then() With Async/Await Syntax ... There's no need to mix the two syntaxes because if a function returns a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is fixed in 2.0.0
No,
.end()
is the old world. Don’t use it..then()
is a replacement for.end()
.