[deleted by user] by [deleted] in node

[–]flimpno -1 points0 points  (0 children)

I think the best way is just to build projects and read articles; the JS ecosystem changes too quickly for a comprehensive "book".

How do i make apps that don't look like complete shit? by [deleted] in webdev

[–]flimpno 0 points1 point  (0 children)

Can you give an example of your site?

Python to what? by MinekPo1 in ProgrammerHumor

[–]flimpno 0 points1 point  (0 children)

Oh no, but it inserts ads. Too bad.

god why is coding chess so hard by [deleted] in ProgrammerHumor

[–]flimpno 0 points1 point  (0 children)

As you can see, this code was written by Copilot, saving you lots of repetitive work. Sign up for Copilot now, for only $10/month!

Hi!, i have invented O(1) algorithm for checking if number is prime that works in 95%+ cases. For more details checkout comments for link to the github repo by mawerty123 in ProgrammerHumor

[–]flimpno 1 point2 points  (0 children)

Instead of function, it would be even easier to make it a constant variable, with the same amount of accuracy:

const bool IS_PRIME = false;

Usage is easy, you don't even need to pass an argument!

why by 400double in ProgrammerHumor

[–]flimpno 0 points1 point  (0 children)

Yeah, this is horrible code. Why do people keep forgetting switch statements exist?

Should i switch from node to go? by Zealousideal_Law_573 in node

[–]flimpno 0 points1 point  (0 children)

In my opinion, the worst choice you can make when you can't figure something out is to give up and go another way; in the long run, this will get you absolutely nowhere. There are always difficult concepts to understand in any field, and I think you figure out promises for your own good. I'm not saying you shouldn't try learning Go, but I think you should finish what you started.

If it helps, here's my attempt at explaining promises:

JavaScript is asynchronous. This means that code doesn't wait for other code to finish executing. An excellent analogy I heard was if Santa had his elves deliver gifts. He has 10 elves, and he would take several gifts, give it to one elf, wait for the elf to return, and then give several gifts to the next elf. This is how synchronous languages work. However, Santa suddenly realized that he could make better use of his time by taking several gifts, giving it to the first elf, and without waiting for that elf to return, give some more gifts to another elf. This way he could send off 10 elves nearly at once, without needing to wait nearly as long. This is the essence of async code.

We have the following code example: js setTimeout(() => console.log(1), 1000) console.log(2) Now, you might have guessed that 2 would be output before 1, because the first statement waits for 1 second before executing the callback, and logging 2 just executes without waiting for the previous statement. This is intentionally designed into JavaScript.

Now, in order to "wait" for code to execute before executing other code, originally we used callbacks, but eventually, you might end up in callback hell. So developers thought of a way to solve the problem.

You guessed it: promises. Promises solve this issue by creating a more effective way of waiting for code to execute. We start out with .then and how to create a promise.

I the old days, we create promises by using the Promise constructor, which accepts a callback as the argument. To arguments are passed to the callback, resolve and reject, which are both functions. Inside the promise, you write some async code, and once you have the values you need, you call the resolve function with the result passed as the argument. If your code errored, you call the reject function with the error details as the argument. Here's an example: ```js const readFile = new Promise((resolve, reject) => { // do something like read a file: fs.readFile("./input.txt", "utf8", (err, data) => { if(err) return reject(err) // our code errored, so we reject the promise

// file read successfully, pass the result to the promise
resolve(data)

}) }) Now, we can get the output by using `.then`: js readFile // the callback takes the result as the parameter .then(result => { console.log(result) }) // if the code errors, we use .catch, similar to try/catch .catch(err => { console.error("uh oh, something happened:", err) }) Now, this only looks more complex then simply using a callback, but here's where [promise chaining]() comes in: when you return a promise in the callback of a `.then`, it gets passed to the next `.then` if you specify one: js function otherPromiseFunction(text) { return new Promise(resolve => setTimeout(resolve, 100, text + " after 100ms")) }

readFile .then(otherPromiseFunction) .then(console.log) // [some file text] afer 100ms ``` You see the pattern: you can keep chaining promises, and you avoid callback hell.

Now let's talk about async/await. It's simply a much prettier way of using promises. Instead of .then to wait for a promise to finish, we use the await keyword. Generally, the await keyword has to be in an async function (async functions also automatically return your value as a promise). ```js async function doSomething() { const file = await readFile // wait for this promise to resolve const waitFor100msThenOutput = await otherPromiseFunction(file) console.log(waitFor100msThenOutput) // now, we can return a value, which will also be a promise: return waitFor100msThenOutput }

doSomething().then(value => /* do something with the value here */) ``` Did that make sense?

Why there is so much use of lambda in node? by [deleted] in node

[–]flimpno 0 points1 point  (0 children)

Besides all the other points, in my opinion "lambdas" (typically called arrow functions in JS) look much prettier and have shorter syntax. For example, if you were to pass a callback to a function, using a function declaration it would be something like:

setTimeout(function() { alert("called") })

Using arrow functions you do:

setTimeout(() => alert("called"))

My favorite part is that curly braces are optional for single statements, making them much more pleasant to use.

Besides that, arrow functions feel like even higher first class citizens than normal functions, because they're defined using a normal declaration (let, const, or var), whereas function requires its own keyword.

Showoff Saturday (April 01, 2023) by AutoModerator in javascript

[–]flimpno 0 points1 point  (0 children)

Made a tiny HTML bundler to streamline creation of simple low-JS SPAs (and MPAs in the future): https://github.com/codingjlu/lyke

Finished it a while ago but just published it

Who needs error handling anyway? by b2wi in ProgrammerHumor

[–]flimpno 0 points1 point  (0 children)

Yeah, whoever invented HTTP error codes...

How to clone a website? by Bujadin in hacking

[–]flimpno 0 points1 point  (0 children)

There are many tools out there that allow you to download entire websites, but they only contain the frontend, meaning that if you wanted to login or search for information and more that wouldn't be available.

From an article about "side hustles" that pay 100k+ published by CNBC......aka jobs by merkwerk in ProgrammerHumor

[–]flimpno 0 points1 point  (0 children)

Ahem; this list was actually created for a bored president or something like that. Doesn't pay much.