0

I have a fetch call which is async of course, but I like to wrap all that in one or more functions, so the calling function doesn't bother.

This is what I have right now:

private async wrapFetch(url): Promise<void> {
    this.responseText = await this.doFetch(url);
}

private async doFetch(url): Promise<string> {
   return fetch(url)
      .then((rsp: response) => {
          return rsp.text();
      });
}

Now, I would like to call wrapFetch as if it is a normal async function

this.wrapFetch('some-url');

But, this line gives the following warning:

Promise returned from wrapFetch is ignored

I've tried to change the return type from wrapFetch, but that made it worse. Any suggestions how I can fix this or should I

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
  • 1
    is that a warning raised by typescript or by IntelliJ (cf https://stackoverflow.com/questions/41278900/intellij-idea-warning-promise-nreturned-is-ignored-with-aysnc-await) ? In any case the error message is referring to the fact that you do not use the value returned by the `wrapFetch` method, if you are inisde an `async` function a simple `await` before the call should be enough, if not you should probably add a `catch` handler to avoid any silent errors – adz5A Nov 26 '18 at 10:44
  • Yes, a warning from my IDE, so its fine to ignore it! – Jeanluca Scaljeri Nov 26 '18 at 11:47
  • Does my comment solve your problem then ? I can make it into an answer – adz5A Nov 26 '18 at 12:06
  • Almost, I tried to reproduce it ([here](https://stackblitz.com/edit/typescript-ljpuv3), but the flow is not right yet, the `console.log('f')` is still before `console.log('e')`. Is there a way to fix this, without using `async/await` inside the `doIt` function ? – Jeanluca Scaljeri Nov 26 '18 at 12:51
  • If you assign the returned value to a correctly typed `var` it seems to be fine. https://stackblitz.com/edit/typescript-1iqahc?file=index.ts Not sure what the underlying issue is though (although I am almost certain it is with the wai ts infers types and not with your code) – adz5A Nov 26 '18 at 13:04
  • Not sure what you mean, what modification to my example can fix the order in which the console.log occur ? – Jeanluca Scaljeri Nov 26 '18 at 13:10
  • 1
    OK I read your comment a little too fast. To fix your problem you just need to make all your methods `async` and add the `await` keyword before method calls (in the `doIt` method for instance...) event if you don't care about the result, just for the control flow, added them in the example https://stackblitz.com/edit/typescript-1iqahc?file=index.ts – adz5A Nov 26 '18 at 13:34
  • Ok, thanks a lot. I hoped that it wasn't needed to add async/await everywhere, but with you answer and rethinking it, it makes sense! – Jeanluca Scaljeri Nov 26 '18 at 14:17
  • You can avoid it by using a `.then(() => console.log))` inside your methods but I am not sure that this what you are looking for. – adz5A Nov 26 '18 at 14:18
  • No, thats not what I need :) – Jeanluca Scaljeri Nov 26 '18 at 14:19

1 Answers1

1

Is that a warning raised by typescript or by IntelliJ (cf Intellij Idea warning - "Promise returned is ignored" with aysnc/await) ? In any case the error message is referring to the fact that you do not use the value returned by the wrapFetch method.

To fix your second problem (the order of the console.log calls) you just need to make all your methods async and add the await keyword before method calls (in the doIt method for instance...) even if you don't care about the result, just for the control flow, I added them in the example stackblitz.com/edit/typescript-1iqahc?file=index.ts

adz5A
  • 2,012
  • 9
  • 10