Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 Assign a variable with an arrow function

I've just started getting accustomed with ES6 syntax and I was wondering if it was possible to assign to a variable with an arrow function. I'm writing a basic lightweight AJAX helper library and on a status of 200, I want to return a payload to the user, which I currently do with:

var responseData = "";
switch (payload.returnType.toLowerCase()) {
    case "json" : responseData = JSON.parse(httpRequest.responseText); break;
    case "text" : responseData = httpRequest.responseText; break;
    default : responseData = null; break;
}
callback(null, responseData);

This is fine, but I can't help but think I could make this cleaner, if I do:

callback(null, () => { switch(payload.returnType.toLowerCase()) { ... });

I would expect the return statement to send the result of the expression as the 2nd parameter in my callback, however when I console log from the caller it prints the switch statement.

Alternatively I have tried to do:

var responseData = () => {
    switch (payload.returnType.toLowerCase()) {
        case "json" : return JSON.parse(httpRequest.responseText); break;
        case "text" : return httpRequest.responseText; break;
        default : return null; break;
    }
}
callback(null, responseData);

In this case, responseData is always empty. Is it possible to have the return value as my 2nd parameter or have it bound to responseData as the result of the arrow function?

like image 727
Halfpint Avatar asked May 14 '16 14:05

Halfpint


People also ask

What does () => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

How do you pass parameters to arrow function?

Enumerate the parameters (param1, param2, ..., paramN) , then put the arrow => , and on the right side write the body { ... } . (number) => { return number * 2; } is an arrow function used as a callback of number.

Can arrow functions have parameters?

Arrow functions can never have duplicate named parameters, whether in strict or non-strict mode.

Are arrow functions ES6?

Arrow functions were introduced in ES6.


1 Answers

I think your hunch is right, but you're on the wrong track, imho. What I think you want to do is create a map of response types to callbacks:

let parsers = new Map([
    ["json", JSON.parse], 
    ["text", (text) => text], 
    ["_", () => null]
]), t = payload.returnType.toLowerCase();

if (!parsers.has(t)) {
   t = "_";
}
callback(null, parsers.get(t)(httpRequest.responseText))

What makes this subjectively "cleaner" is that you separate logic from implementation. You can move the parser definition anywhere without affecting the code. That's why switch statements feel "unfunctional" (or undeclarative).

But of course, this all remains a matter of taste :)

like image 110
Gerard van Helden Avatar answered Oct 13 '22 16:10

Gerard van Helden