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

10
  • 1
    is that a warning raised by typescript or by IntelliJ (cf stackoverflow.com/questions/41278900/…) ? 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 Commented Nov 26, 2018 at 10:44
  • Yes, a warning from my IDE, so its fine to ignore it! Commented Nov 26, 2018 at 11:47
  • Does my comment solve your problem then ? I can make it into an answer Commented Nov 26, 2018 at 12:06
  • Almost, I tried to reproduce it (here, 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 ? Commented Nov 26, 2018 at 12:51
  • 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 stackblitz.com/edit/typescript-1iqahc?file=index.ts Commented Nov 26, 2018 at 13:34

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.