0

In my code I'm fetching the data then processing JSON inside it like this:

new BluebirdPromise((resolve, reject) => {
  fetch(url)
  .then(response => {
      response.json().then(data => ({
        resolve();
    })
  })
  .catch(console.log);
});

this gives me a warning:

Warning: a promise was created in a handler at handler but never returned from it

Im still getting the warning even if i do it like this:

return fetch(url)
  .then(response => {
      return response.json().then(data => ({
        resolve();

      //to make sure warning will not show up
      return null;
    })        
  })
  .catch(console.log);

Am I missing something here?

The reason I'm creating a bluebird promise is because I need an ability to cancel the request. Within this promise I have a fetch that creates another promise (non-cancelable). Besides that I have additional logic after the fetch resolves, that analyzes the returned result & resolves or rejects the BluebirdPromise. This still generates a warning:

return new BluebirdPromise((resolve, reject) => {
            return fetch(EndPoint, {})

                .then(() => {
                    return resolve(1);
                })
                .catch();
});
}
AnKing
  • 1,994
  • 6
  • 31
  • 54
  • https://stackoverflow.com/questions/34370957/bluebird-warning-a-promise-was-created-in-a-handler-but-was-not-returned-from-i - maybe the same – Alejandro Nov 28 '18 at 19:41
  • also, in hover example you seems to never call `resolve`, try apply it – Alejandro Nov 28 '18 at 19:47
  • @Alex I edited the question to show resolve. It doesn't however makes a difference to the warning generation – AnKing Nov 28 '18 at 20:47
  • You should not use `new BluebirdPromise` at all. – Bergi Nov 28 '18 at 22:24
  • @Bergi, I went thru the referenced answer and unsure how to apply it in my case. My function needs to return promise that is cancellable. within that promise I have 2 nested promises: fetch > json. json is the one that resolves the main promise. But if I just return fetch then I will not be able to cancel it, because it is not a bluebirdPromise? – AnKing Nov 29 '18 at 14:23
  • You're not able to cancel the fetch request anyway? If you all you want is a Bluebird promise for the native promise's result, use `return BluebirdPromise.resolve(fetch(endpoint).then(response => response.json()));` – Bergi Nov 29 '18 at 21:49
  • @Bergi First, I think fetch can be aborted. But I haven't figured out how to pass a call to inside of a running promise to do fetch.abort(). But the reason I'm trying not to return fetch directly is because I want to analyze json response within this cancellable promise. I'm using this promise to make various calls that can return error codes(within json). I want to process all errors in a single spot.(inside my static function that returns this Bluebird promise). Maybe I'm not doing it right? Is there a better solution? – AnKing Nov 30 '18 at 14:11
  • You can still do `.then(result => { /* analyse, throw/return error codes */ })`. You don't need a promise constructor for that - see the duplicate. Yeah, if you want to install a cancellation listener you might have to do something with more Bluebird-specific features. – Bergi Nov 30 '18 at 19:12

0 Answers0