question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[fixed]The .then() function is not compatible with Promises

See original GitHub issue

The 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:closed
  • Created 8 years ago
  • Comments:33 (17 by maintainers)

github_iconTop GitHub Comments

6reactions
kornelskicommented, Jun 27, 2016

This is fixed in 2.0.0

4reactions
kornelskicommented, Sep 19, 2016

No, .end() is the old world. Don’t use it. .then() is a replacement for .end().

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found