As a traditional (non-script) developer, who always starts the code with proper class, methods etc in C++/Java,
class ApiClient
{
ApiClient(){}
getSomething(){}
putSomething(){}
}
This always looked so clean for my eyes.. Now compare it with JS/Typescript code :
class ApiClient {
public get(apiURL: string): Promise{
return new Promise((resolve, reject) => {
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
// Add any other headers as needed
},
};
const request = http.request(apiURL, options, (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
if (response.statusCode === 200) {
const responseData = JSON.parse(data);
resolve(responseData);
} else {
reject(new Error(\`Error: ${response.statusCode}, Response: ${data}\`));
}
});
});
request.on('error', (error) => {
reject(error);
});
request.end();
});
}
}
Why do they want to write the whole code into the ()? It's almost like writing all the code inside (..) in a C++ function.. where we just expect the params & arguments. like:
ApiClient(.....Write the whole code here? What? :( ){}
Seriously I have so much pain tracking where the brackets begin and end if I'm reviewing a .js code snippet. Did C++/Java also move into this Anonymous function hype? Looks like I'll have a pretty steep learning curve on any direction. Hating this. :| How did you survive this? If you happen to a non-JS developer migrating into the JS world?