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.