17

What is the actual impact of putting void before promise?

async function doAsyncStuff(){
...
}

function nonAsyncFunction(){
  void doAsyncStuff();
}

I couldn't find any official documentation for this, but it must be doing something as it resolves no-floating-promises TSLint error.

JeB
  • 11,653
  • 10
  • 58
  • 87
  • 1
    how about documentation for the void operator? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void – Jaromanda X Apr 07 '19 at 12:11
  • Yep, that's exactly what I was looking for. Tried to find it in Promise docs, apparently I wasn't looking in the right place. – JeB Apr 07 '19 at 12:17
  • yeah, `void` has been a javascript operator since Brendan Eich wore short pants :p – Jaromanda X Apr 07 '19 at 12:18
  • See also: [Can I fire and forget a promise in nodejs (ES7)?](https://stackoverflow.com/q/32384449/1048572) – Bergi Apr 07 '19 at 12:39
  • I personally prefer `/*await*/ fireAndForget();` cause that is also understood by people that aren't that familiar with JS, it doesn't stop the linter from complaining though – Jonas Wilms Apr 07 '19 at 13:04

1 Answers1

24

void is an operator that accepts a value on the Right-Hand Side and evaluates as undefined.

It resolves no-floating-promises because it does something (or rather, explicitly nothing) with the promise.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Oh wow, TIL people were using `void` to suppress the rule. Filed https://github.com/palantir/tslint/issues/4653. Thanks for this @Quentin! – Josh Apr 10 '19 at 14:19
  • 2
    ^ or, how to render the answer incorrect in one easy step. – Phil Feb 15 '22 at 09:29
  • Looks like this behaviour can be had back with some config, at least with typescript-eslint. See https://typescript-eslint.io/rules/no-floating-promises – Phil Feb 15 '22 at 10:43