CYB
CYB

Reputation: 1106

a promise was created in a handler but was not returned from it in bluebird

I'm using bluebird in my app and I use babel to compile my code into es5. However, I always got this warning and I've checked that every pieces of Promise has return value.

Here's my code:

Promise.promisifyAll(fs);

use.login().then((result) => {
  console.log(result);
  doSomething('../test.png');
});

function doSomething(filepath) {
  return fs.readFileAsync(filepath).then((bufs) => (
      doPost(url, bufs, filepath)
        .then((res) => (
          res.error ? Promise.reject(res.error) : Promise.resolve(res))
        )
  )).catch((err) => {
    console.error(err);
    return err;
  });
}


function doPost(url, bufs = null, filepath = null) {
  return new Promise((resolve, reject) => (
    unirest.post(url)
      .headers(config.Headers)
      .timeout(120000)
      .field(bufs)
      .attach('files', filepath)
      .end((res) => (
        res.error ? reject(res.error) : resolve(res)
      ))
  ));
}

Details of error messages:

Warning: a promise was created in a handler but was not returned from it
at doSomething (/home/test/Documents/test/lib/abc.js:2:27)
// This line number is referred to the compiled code which is equal to line 4:12 in the above code

Upvotes: 2

Views: 1617

Answers (1)

CYB
CYB

Reputation: 1106

Thanks @Bergi that I didn't check the promise stack where I call doSomething so the warning disappeared after I modify my code as below

use.login().then((result) => {
  console.log(result);
  doSomething('../test.png');
  return null; // Add this line to make promise return anything, then the warning gone
});

Upvotes: 0

Related Questions