8

I have come across this code:

return someFunctionThatReturnsAnArray()
  .map((action) => void dispatch(action))
  .run();

Can anyone suggest why you would prefix void in the map function so that it returns undefined each time?

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
dagda1
  • 26,856
  • 59
  • 237
  • 450
  • 7
    looks like they don't know about `forEach` – Rhumborl Apr 11 '18 at 15:59
  • 6
    Maybe `run()` **needs** an array of `undefined`, eh? – sleighty Apr 11 '18 at 15:59
  • 1
    Possible duplicate of [Why use the void keyword?](https://stackoverflow.com/questions/15231601/why-use-the-void-keyword) – 31piy Apr 11 '18 at 16:02
  • 1
    he knows what void means. reread the question. – omikes Apr 11 '18 at 16:04
  • 2
    `forEach` does not return an array so its not the same. What is more disturbing in this code it looks like they monkey patched `Array.prototype` with `run` function. – Moti Korets Apr 11 '18 at 16:04
  • 2
    yeah the answer appears to depend on adding the source of the magical `run()` function – Rhumborl Apr 11 '18 at 16:06
  • 1
    at best the `run()` function could gather the length of the array from this. perhaps the programmer has an affinity for one line functions? the answers below amount to "i don't know" so i believe it shall remain a mystery. – omikes Apr 11 '18 at 16:11
  • Or maybe it's not an Array but something else with a map function that may have different semantics. – Jacob Apr 11 '18 at 16:58
  • I think it's safe to assume that's an array given that the function is called `someFunctionThatReturnsAnArray()`? – sleighty Apr 11 '18 at 17:54
  • Prefixing any function call with the `void` keyword discards the returned value. – Racil Hilan Apr 11 '18 at 15:59

3 Answers3

5

This is essentially a clever way of defining a function that returns undefined.

From MDN:

The void operator evaluates the given expression and then returns undefined.

Your code is essentially the same as:

return someFunctionThatReturnsAnArray()
  .map((action) => { dispatch(action); })
  .run();

I'll be honest: if I came across that code in any project that I work with, I'd flag it for review. It doesn't really make sense and is potentially confusing.

rossipedia
  • 56,800
  • 10
  • 90
  • 93
1

void operator allows evaluating expressions that produce a value into places where an expression that evaluates to undefined is desired.

So it looks like :

  • as long as the dispatch method is called, its return value is not important
  • the result of the map method is an array with undefined values but its length is equal to the number of dispatched calls
  • the run method is not standard for the Array prototype (seems like a custom patch), therefore it is hard to tell what it does.

One of its usage would be info of dispatched calls

Array.prototype.run = function() {
  console.log("Dispatched calls : " + this.length);
};

var getArray = function() {
  return ['1', '2', '3'];
}

var dispatch = function(n) {
  return 10*n;
}

getArray().map((action) => void dispatch(action)).run();
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • But why would anyone need to discard the values of the array? – sleighty Apr 11 '18 at 17:52
  • @BrunoEly I am not sure what was the author intention as the `run` method is not provided. One reason would be to indicate that `dispatch` method result is irrelevant and should not be used later on in the code?? – MaxZoom Apr 11 '18 at 18:06
0

Taken from https://eslint.org/docs/rules/no-void

The void operator takes an operand and returns undefined: void expression will evaluate expression and return undefined. It can be used to ignore any side effects expression may produce:

The common case of using void operator is to get a “pure” undefined value as prior to ES5 the undefined variable was mutable:

// will always return undefined
(function(){
    return void 0;
})();

// will return 1 in ES3 and undefined in ES5+
(function(){
    undefined = 1;
    return undefined;
})();

// will throw TypeError in ES5+
(function(){
    'use strict';
    undefined = 1;
})();

Another common case is to minify code as void 0 is shorter than undefined:

foo = void 0;
foo = undefined; 

When used with IIFE (immediately-invoked function expression), void can be used to force

the function keyword to be treated as an expression instead of a declaration:

var foo = 1;
void function(){ foo = 1; }() // will assign foo a value of 1
+function(){ foo = 1; }() // same as above
function(){ foo = 1; }() // will throw SyntaxError

Some code styles prohibit void operator, marking it as non-obvious and hard to read.

Can't see any good reason to be using it in the snippet that you have provided.

Xotic750
  • 22,914
  • 8
  • 57
  • 79