0

I'm working with Angular 8 and I'm facing this message:

'await' has no effect on the type of this expression.ts(80007)

What I'm trying to understand is the behavior of await on void functions.

Example:

const func1 = () => console.log(1);
const func2 = () => console.log(2);
const func3 = () => console.log(3);

// As you can see above those 3 functions are void

const asyncfunc = async () => {
await func1();
await func2();
await func3();
}

From what I read on the web using await on void function will return an undefined wrapped in a promise,

So that means await does effect in this case?

If so, then why I'm getting the message that await does not effect in this case.

is it only tslint or I misunderstood the behavior of await on void functions?

Do I have to return a promise from those function the achieve the effect of the await?

Tomerz
  • 93
  • 1
  • 9
  • 2
    None of the `funcX` functions returns a promise. What should be the effect? – Andreas Jul 06 '20 at 07:27
  • Clarification on the above... [_"The await operator is used to wait for a **`Promise`**"_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) – Phil Jul 06 '20 at 07:30
  • 2
    *"From what I read on the web using await on void function will return an undefined promise,"* Sort of, yes. `await x` basically does `Promise.resolve(x)` and then waits for that promise to settle. If `x` isn't a *thenable* (loosely, a promise), the promise from `Promise.resolve` is already settled and `await` just has to wait very briefly. When you call a function that doesn't return anything, you get the value `undefined`. So `await fn()` where `fn` doesn't return anything awaits a settled promise with the fulfillment value `undefined.` It's not quite true that `await` does nothing in... – T.J. Crowder Jul 06 '20 at 07:33
  • ...your code above, but what it does is unlikely to be what you want it to do. – T.J. Crowder Jul 06 '20 at 07:33
  • Hello @Phil , I have already read this, and it does not answer my question. In the post you have mentioned the function sleep returns promise that not my case!! – Tomerz Jul 06 '20 at 07:44
  • 1
    @TomerZaidler - It does a couple of things. 1. An `async` function runs synchronously until it returns or it reaches the first `await`; that's when it returns its promise. So using `await x` where `x` isn't a thenable ends the synchronous portion of the `async` function. You can see that here: https://jsfiddle.net/tjcrowder/tb9o4g68/. 2. Even if it's not the first `await` in the function, it has to wait until the next time microtasks are processed because promise settlement handlers are always called asynchronously. So it always introduces an "async tick" delay into the code. – T.J. Crowder Jul 06 '20 at 08:35

0 Answers0