What do you mean by "any subsequent errors and then fail"? If the original rp promise is not fulfilled, catch is executed ... during the failure. Once a promise is rejected, it cannot be "subsequent errors."
In addition, it should look like a statement (e.g. from chai ) that suggests that you are trying to verify this. Chai should.throw does not should.throw error, it checks that an error has been selected. If you are testing this, you need to point to the test ( it block) that the test is asynchronous and not synchronized - usually by assigning a name and calling done . Otherwise, the request will be sent, and then before any response is executed, the script will be synchronously completed and no errors will be listened.
What else, are you speculating that something should throw to start the console, but nothing in your code throw s! If you pointed to throw , you should understand that throw inside then or catch will simply reject the outgoing promise from this handler with the given value (yes, catch export the new promise, just like then - this is 100% sugar for .then(null, errHandler) . If you want errors to be returned back to the window, you need to complete the chain using the promise method Bluebird .done() , accessed by request - ask through several arcane .promise().done() . But even in this case, you will still need to indicate that you are running asynchronous ones Art.
In short, itβs not entirely clear what you think about what this code should do, and how it differs from your expectations. Please clarify!
var rp = require('request-promise'); rp.get({ // start an async call and return a promise uri: 'http://httpstat.us/500', transform: function(body, res){ res.data = JSON.parse(body); return res; } }).then(function(res){ // if rp.get resolves, push res.data results.push(res.data); }) .catch(function(err){ // if rp.get rejects (eg 500), do this: should.throw.error.to.console(); // test if something is thrown (but nothing has been!) var respErr = JSON.parse(err.error); var errorResult = { origUrl: respErr.origUrl, error: respErr }; results.push(errorResult); // push an object with some of the error info into results }); // this line (eg, end of script) is reached before any of the async stuff above settles. If you are testing something, you need to make the test async and specify when it complete by invoking `done()` (not the same as ending the promise chain in Bluebird `.done()`).
source share