r/badcode • • Apr 25 '23

js Let me die please.. NSFW

Post image
133 Upvotes

26 comments sorted by

60

u/someone-else-1506 Apr 25 '23

Am I dumb or this code make no sense? I can't even see the problem with it.

34

u/mrbraises Apr 25 '23

It’s not about what the code is supposed to do, it’s about everything else. The top level new Promise that contain all the require, it’s just non sense. Below this code there is also this : new Promise((resolve, reject) => axios.get(url).then(res => resolve(res.data)).catch((err) => reject(err))).catch((err) throw err) I hate my job sometimes

31

u/thislooksfun1 Apr 25 '23

A top-level promise isn't that bad if you want to do async stuff at the top level, but still need to run in environments that don't support top-level await. Using require in a scope is also fine—unusual, sure, but fine—it's even necessary sometimes, which why ES6 import has a function version.

That being said, nesting promises, like in this comment, that's unforgivable. If you do that in prod code wt-actual-f is wrong with you.

9

u/mrbraises Apr 25 '23

It is production code that’s why I posted it! There is like 20 scripts that are following this structure (they made a template file with this as a base..). Pray for me

13

u/GergiH Apr 25 '23

Feels like they just hopped the framework train with developers having no experience with it (or just JS in general). I had been on a lot of legacy .NET projects lately, and all their JS/JQuery feel similar. On a second thought tho, all their C# code looks shit too.

1

u/someone-else-1506 Apr 26 '23

😐😐😐😐😐😐

1

u/LegacyCrono Apr 27 '23

Nothing wrong with nested promises. Async funcs all return their own promises after all, so they naturally are nesting as they're called. The weird part is not awaiting that second promise, or not having that as a separate async function...

1

u/Ythio Apr 26 '23

I think they're trying to load dependencies inside an asynchronous process.

Not a JS dev though, could be wrong.

11

u/lHeliOSI Apr 26 '23

TODO: Bien décrire le processus

Lol

7

u/mrbraises Apr 26 '23

Le truc est là depuis 5 ans ça me fume vraiment

3

u/galexj9 Apr 26 '23

I've been working with promises a lot lately and I don't think this will even work???

if(err) reject(err) is missing a return statement so it will continue to resolve(items) anyway.

WHY? Are there import/require statements inside a promise??

Edit: just saw the promise isn't wrapped in a function or even assigned to a variable, just floating in the main body of code. What the fuck

3

u/mrbraises Apr 26 '23

They are insanely lucky, reject does not stopped the execution but the resolve will trigger nothing if a reject happened before. For the import/require I never had the answer.

If you want to throw up a bit more, after this big new Promise they made a .catch((err) => and inside it they put all the require again to use it for the error handling

3

u/PixelPerfect41 Apr 25 '23

I'm not that high into js to understand the weirdness here 💀

5

u/mrbraises Apr 25 '23

Wrap all your code into a new Promise instead of doing a function is usually not that good of an idea (+ requiring inside your callback 🫠)

20

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 25 '23

Image Transcription: Code


[The buildDataToPost function in the following code appears to be collapsed, and thus none of its code is visible. The visible code reads as follows:]

new Promise((resolve, reject) => {
  const csv = require("csvtojson");
  const axios = require("axios");
  const path = require("path");
  const fs = require("fs");
  const yyyymmdd = require("../../../helpers/yyyymmdd");
  const addDt = require("../../../helpers/addBusinessDays");
  require("dotenv").config({ path: path.join(__dirname, "../../../.env") });
  
  /**
   * Function to read a CSV file and prepare the array of JSOn to post
   * @param {string} fileName : file name for the CSV file to read and download
   *
   * Return a Promise than resolves an array of JSON ready to be posted
   */
  function buildDataToPost(fileName) {
  }
  
  /**
   * Main code part here
   * TODO:décrire bien le process
   */
  new Promise((resolve, reject) => {
    // GETTING A LIST OF ALL FILES IN THE DIRECTORY
    fs.readdir(filePath, function (err, items) {
      if (err) reject(err);
      resolve(items);
    });

I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!

7

u/siren1313 Apr 26 '23

Good human

5

u/Select_Property3162 Apr 25 '23

are you required to die?

7

u/anurat- Apr 26 '23

new​ Promise(() => { require('die'); })​;

1

u/[deleted] Apr 26 '23

Why don't you just quit and find another job?

3

u/mrbraises Apr 26 '23

Because it pays two times more than the average developer job, I’m a money slave

8

u/Feathercrown Apr 25 '23

This isn't that bad. Using new Promise instead of async function is weird for sure though

0

u/geon Apr 25 '23

Just refactor to use async. If it looks weird, that is likely a bug, so fix it.

3

u/mrbraises Apr 25 '23

I’m not asking for advice I’m just posting some weird stuff I have to deal with on day to day basis because the codebase was made by non developer people

1

u/zombarista Apr 26 '23

If you are using a “project file” like package.json, etc, use find-up to traverse upward and find your file!

1

u/RootU5R May 06 '23

The whole code seems to me inside the first promise. Loading require and having a nested promise which use the require modules. This is really bad.

Time to change it to asnyc await

0

u/pixelpuffin Apr 26 '23

I felt a lot less like dying since I shifted to coding in other languages more. JS is a cancer of an environment.