-1

In the following code, calling the resolve() at point (A) does not result in execution of the then() clause of the call to createRequest(). The code is getting to the point of the resolve() call, which I verified by the print statement.

const request = require('request');

function createRequest() {
  return new Promise(function(resolve, reject) {

    // Setup request
    // ...

    request(options, function(error, response, body) {
      if(!error && response.statusCode == 201) {

        // If the resolve() is done at this point, the then() clause is executed

        uploadFiles(files)
          .then(handles => {
            console.log('handles: ' + handles); // This is printed

            resolve('ok'); // (A) Then clause in call to createRequest is not called
          })
          .catch(reject(requestObj)); // (B) I didn't have this in my initial post
      }
    });
  });
}

function uploadFiles(files) {
  return new Promise(function(resolve, reject) {
    // Upload files
    // ...

    resolve(handles);
  });
}


createRequest()
  .then(message => console.log(message) /* This never executes */)

[edit]

I added a catch() clause to my createRequest() call, and I was surprised to see something get printed, which was strange because the handles: statement was getting printed. I changed (B) to the following:

.catch(() => reject(requestObj));

and my problem was solved. The then() in the call to createRequest was getting executed. Not sure why this is happening.

user2233706
  • 6,148
  • 5
  • 44
  • 86
  • 1
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Feb 22 '19 at 15:40
  • Have you maybe already called `reject()` elsewhere? Otherwise no, if you get the `handles:` log and the `resolve('ok')` is executed, then the `then` callback will also run. – Bergi Feb 22 '19 at 15:40
  • I'll look at that question, but I'm not calling reject() anywhere. The ```handles:``` print should mean the ```resolve()``` is executed. – user2233706 Feb 22 '19 at 15:42
  • Check agian owner of `resolve` variable, maybe your `resolve` (`resolve('ok')`) not related to your promise. – hoangdv Feb 22 '19 at 16:19
  • @user2233706 Yes it should. When I execute your code, it works for me. Given that you omitted some things, I'll guess the culprit is elsewhere. Can you post your complete actual code, please? – Bergi Feb 22 '19 at 16:40
  • Yes, I hadn't posted my complete code. – user2233706 Feb 22 '19 at 17:01
  • Instead of `request`, use the already (correctly) promisified `async-request`. – Roamer-1888 Feb 22 '19 at 17:04
  • `.catch(reject(requestObj))` does indeed call `reject`, it's different from `.catch(() => reject(requestObj))`. – Bergi Feb 23 '19 at 21:51

1 Answers1

-1

This is because catch() expects a function, not a function call. Even though the catch() clause is not executed, NodeJS still expects it to have a valid function. If not, the promise is rejected by NodeJS. For example:

const request = require('request');

function createRequest() {
  return new Promise(function(resolve, reject) {

    var options = {
        url: 'https://www.google.com'
    };

    request(options, function(error, response, body) {
      if(!error && response.statusCode == 200) {    
        uploadFiles()
          .then(handles => {
            resolve('ok');
          })
          .catch(reject('bad')); // (A)
      }
    });
  });
}

function uploadFiles() {
  return new Promise(function(resolve, reject) {
    resolve([1, 2, 3]);
  });
}


createRequest()
  .then(message => console.log(message))
  .catch(message => console.log(message));

This results in 'bad' getting printed. If (A) is replaced with .catch(() => reject('bad'));, then 'ok' is printed.

user2233706
  • 6,148
  • 5
  • 44
  • 86