void before promise syntax
Asked Answered
O

1

25

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.

Outstrip answered 7/4, 2019 at 12:10 Comment(5)
how about documentation for the void operator? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Sutphin
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.Outstrip
yeah, void has been a javascript operator since Brendan Eich wore short pants :pSutphin
See also: Can I fire and forget a promise in nodejs (ES7)?Cromagnon
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 thoughShill
D
31

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.

Doloritas answered 7/4, 2019 at 12:12 Comment(3)
Oh wow, TIL people were using void to suppress the rule. Filed github.com/palantir/tslint/issues/4653. Thanks for this @Quentin!Rockrose
^ or, how to render the answer incorrect in one easy step.Rhoda
Looks like this behaviour can be had back with some config, at least with typescript-eslint. See typescript-eslint.io/rules/no-floating-promisesRhoda

© 2022 - 2024 — McMap. All rights reserved.