[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Is Javascript good now? const f = async () => ( new Promise((resolve,reject)=>(

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 118
Thread images: 7

File: js.png (17KB, 200x200px) Image search: [Google]
js.png
17KB, 200x200px
Is Javascript good now?

const f = async () => (
new Promise((resolve,reject)=>(
setTimeout(()=>{resolve(Math.random())}, Math.random()*3000)
))
)

async function kek() {
console.log('Waiting...');
let r = [
await f(),
await f(),
await f(),
await f()
]
console.log('Done...');
console.log(`Result: ${r}`);
}

kek()
>>
I'm not sure why this exists when you could just do Promise.then
>>
File: 1499412711787.jpg (13KB, 142x166px) Image search: [Google]
1499412711787.jpg
13KB, 142x166px
>>61367233
>53-bit integer that only do 32-bit bitwise arithmetic
>good
>>
>>61367248
>2017
>even using bitwise arithmetic
>>
>>61367269
>what is crypto
Have you even seen the shit libsodium.js and sjcl have to pull?
>>
Javascript is even more of a clusterfuck than sepples at this point
>>
>>61367246
can it do what op did, putting async calls where you'd normally have sync and block the thread? lf not, well, there's your answer.
>>
>>61367300
>2020-3
>cyrrupto
What have you got to hide, goy?
>>
>>61367233
ES2015 is really comfy to use desu
- arrow functions
- const, let / no retarded hoisting
- async, await


just use eslint and don't fall for the typescript meme and you're gonna have a good time
>>
>>61369236
> don't fall for the typescript meme
elaborate
>>
>>61369245
Not that poster but I read (the URLs for) studies that showed that static typing didn't improve code quality.
>>
>>61369245
There are other options for (runtime) static typing if you want to (propTypes being my fav, but there's Flow and bunch of other shit)
I just don't want to introduce shitty MS tooling into my codebase.
>>
>>61369261
it doesn't improve quality, but at least it prevents many bugs due to the type system
>>
the new string interpolation and generator / lambdas are too good, js came a long way
>>
>>61369294
You mean `this {shit}` ?
Back ticks are ugly, I prefer
$"C# {syntax}"
>>
>>61369236
oh also I forget the whole destructuring, spreading and rest operators
also default parameters, super useful to write functions with one mandatory argument and whatever optional shit you can think of via an object argument with default parameters.
no more refactoring baby
shit is cash
>>
>>61367233
ES6 and immutable.js made it a good language, yes.
>>
>>61369411
> immutable.js
Seems like a meme to me.
Why not just use spread operator?
Object stays serializable and you don't introduce some 3rd party library
newState = { ...oldState, foo: 'bar' }
>>
>>61369411
>immutable.js
>not writing vanilla
You fucked up bro
>>
>>61369281
Doesn't it also make it easier to document your code?

I don't use TS (yet) but I assume doc systems automatically pick up the types?
>>
>>61367233

I legitimately don't get what the JS community is smoking. I mean I get that this is "better", but this is fucking horrible.
>>
>>61369489
I write JS in my freetime and also get paid to write JS.
Modern JS is sand-castles built upon sand-castles.
To use a modern JS framework you have to install and maintain at least five other modern JS frameworks/libraries. It's the modern PHP
>>
>>61369441
>>61369443

Immutable.js is to replace data structures like Map, List, Set, etc.
Not plain objects.

You can write much cleaner code using these data structures than you can with plain objects.

And it prevents bugs while being much faster than the build in structures.
>>
>>61369537
>replace data structures like Map, List, Set
I don't use that shit anyway, it's all sugar for primitives.
>>
>>61369552
>I don't use that shit anyway

You really should.
It makes such a huge difference in your code:
No more need for temporary variables, for/while loops and all that mess.

It's the difference between writing JS like a Pajeet or like a professional.
>>
>>61369512
Yeah, if by "modern" you mean "not yet released".
You can use es6 imports, let, promises and many other stuff, and if you aren't allergic to libraries you can sacrifice a couple lines for co which lets you use generators in place of async/await.
>>
>>61369632
No thanks.
I don't care about line count or variable length.
Same reason I don't use linq in my C# ; if I can't tell what it's doing why use it? Every abstraction adds complexity and inefficiency.
>>
>>61369785
>adds complexity and inefficiency.

It's easier to write and especially to read, and FASTER to execute.

But then you don't care about that do you, Pajeet?
No, keep fucking around with for loops and temporary variables.
>>
>>61369838
>FASTER to execute
If you say so boss.
>>
>>61369021
Not by a fucking long shot
>>
>>61369179
It can. Asynchronous/Await is just syntactic sugar over promises. Just go see what your transpiler does with your code to make it work in es5 browsers.
>>
>>61367233
JavaScript is the epitome of a bad programming language.

Programmers generally think that it is a good thing that a language has been adopted universally, but in the case of JavaScript it is so far from the truth. The fact that there is javascript executing in every modern browser is a little disturbing when you consider that malware written in javascript could, in theory, effect everyone.
>>
>>61369236
>ES2017
Ecmascript 2017 was officially done few days ago. async await is part of it.
>>
>>61369919
So then C and C++ are far worse than javascript by your definition of a bad language.
>>
>>61367233
new js is shit, fuck it
>>
How do I web development in my free time without javascript ? I don't want to get into this ES mess but every semi-basic functionality like dropdown menu is much easier with JS.
>>
>>61369918
>Asynchronous/Await is just syntactic sugar over promises.
Nope.
>>
>>61369919
Browsers have a sandbox. While there have been exploits capable of escaping it, it depends on engine bugs and different engines have different bugs.

The real issue here would be everyone moving to webkit + V8.
>>
>>61370001
Either use a language that compiles to JS, create your own language that compiles to JS, or write a browser that doesn't use JS.
>>
File: 1493761273856.gif (3MB, 300x252px) Image search: [Google]
1493761273856.gif
3MB, 300x252px
>>61369919
>malware written in javascript
>>
>>61370001
You could build regular pages that don't do anything on the client side and maintain state in the server side instead.

Simply wait for requests and return responses.
>>
>>61369918
I don't see it. you'd still need to write callbacks to achieve what is done in op without any callbacks.
>>
>>61370045
>He doesn't know Windows script host
>>
>>61370045
We had that already, something to do with pdf.js in modern browsers.
>>
>>61370045
Yes, basically all of it that's effective.
Please educate yourself :^)
>>
>>61370065
Yes. That's why it is syntactic sugar. You can write it in the ugly callback hell way or using async await. But in the end you can do the same things.
>>
>>61370046
Fun!
>>
File: 1386477082286.gif (2MB, 430x221px) Image search: [Google]
1386477082286.gif
2MB, 430x221px
>>61370020
>>
>>61370085
Once again, no, async/await is not syntactic sugar over promises. Async/await allows a function to wait for a promise to fulfill without blocking the entire thread.
>>
>>61370085
You can do anything you want without ever using your own callbacks. Don't downplay the importance of those things by saying they are syntactic sugar - they are proper new syntax that is convenient to use.
>>
>>61370114
>Async/await allows a function to wait for a promise to fulfill without blocking the entire thread
Isn't that what a Promise does?
>>
>>61370114
Async functions return promises
>>
>>61370130
No, a promise is not capable of pausing the execution of a function. Async/await makes it possible for a function to literally wait until a promise resolves, then continue going from that point.
>>
>>61370123
>they are proper new syntax that is convenient to use.
That's the definition of syntactic sugar.

I'm not saying they are bad. They are great and reduce the amount of code you have to write a lot and reduce bugs a lot. But still.
>>
>>61370199
So what does a promise do?
>>
>>61370130
Cont. To be clear, it WAS possible to implement before, but only if you used generators and the "yield" statement together with promises. Async/await allows you to implement this directly.
>>
>>61370199
Async/await is literally implemented on top of Promises. It is just syntactic sugar for them to make it look like coroutines.
>>
>>61370130
Promise allows you to call another function, a callback. async/await allows you to stop execution of current function without blocking and resume execution where you left after the async operation is done. The resuming part is not achievable with promises - you work around that by splitting your function into multiple small ones. Also see call/cc.
>>
>>61370230
Async/await uses new functionality that could not have been implemented in JS before EXCEPT by using generators/yield, which are themselves fairly recent additions to the standard. These features CANNOT be implemented using just promises and not, at minimum, also generators in addition.
>>
>>61370221
Calls a function you specify after certain conditions are satisfied. No resuming, calls the function from its very beginning.
>>
>>61370210
No, new syntax is not always just syntactic sugar.
>>
>>61370246
Then how do they work when transpiled to es5, genius.
>>
>>61370276
That's not the same thing at all. When you compile to language you can do things you can't implement in that language natively. Python can be compiled to C and python has closures, that doesn't mean you can implement closures in C.
>>
>>61370276
Probably the functions are broken up into smaller functions by the transpiler.
>>
>>61370239
That's exactly what the ".then" part of the promise does.
>>
>>61370315
>When you compile to language you can do things you can't implement in that language natively
I don't understand how that's possible
>>
>>61370326
No. When you call .then() on a promise, the function in which you make the call continues to execute without waiting for the promise to resolve.
>>
>>61370326
then allows you to call another function from its very beginning, no resuming involved. Please stop pretending to be retarded.
>>
>>61370331
It isn't.
>>
>>61370331
Read my post till the end. Do you agree with my reasoning about closures in C?
>>
>>61370336
>the function in which you make the call continues to execute without waiting for the promise to resolve
This isn't really true. The execution continues, yes, but when the promise returns the code in the .then clock executes.

doWorkAsync(param)
.then( result => {
// this happens when dWA finishes
})
// anything here happens right after dWA is called
>>
>>61370394
>this isn't true
>proceeds to demonstrate it literally is true
I usually am very polite in conversations on 4chan but how do you not call person a retard after a post like that?
>>
>>61370446
Not him but what the fuck are you talking about?
What he posted was 100% correct AFAICT.

Also how come people who are wrong always resort to insults?
>>
>>61370566
He admitted the execution of outer function continues right after the call to dwa, something that me and other anon have been telling him along - and something that doesn't happen with async await. I resort to insults because i literally told him this multiple times as detailed as i could, and he still doesn't see it.
>>
>>61370608
>the execution of outer function continues

So?

Just leave it empty if that brothers you.
This argument was abut the part that goes inside the .then

All you've shown is that promises are more versatile than await.
>>
>>61370672
The only thing that is bothering me is your ignorance. We are talking about conceptual difference between promises and async await. Promises only allow you to call a function after certain conditions are met. async/await interrupts execution of function without blocking the thread it's in, and later resume the execution where it stopped - something promises don't do and can't do.
>>
>>61370716
>Promises only allow you to call a function after certain conditions are met
This was the very thing that was upsetting you in my dWA example above.

There are things which MUST happen sync, and things which can happen async. Promises let you do both, or either. Below is contrived...

// assuming new user details are legit
createNewUserAsync(username, pass, email)
.then( loginLink => {
emailNewUser(username, email);
});
forwardUserToDashboard();


Emailing the user can happen any time, but you want them to be forwarded right away.
>>
>>61367233
It's the necessary evil, unlike java that is the unnecessary but still used as fuck evil,.
>>
>>61370827
Java > JavaScript.
Java accomplishes exactly what it intends to do, JavaScript is an overengineered and hacked together turd.
>>
>>61370796
I edited this post for like 15 minutes and now it makes no sense because it's a Frankensteins monster. fuck this thread anyway, it's friday night
>>
>>61370796
I don't see why you're trying to convince me that promises are better. I'm not arguing in favor of any, I'm arguing that promises and async/await are conceptually different, something you or anon you took place of claimed to be false.
>>
>>61370846
But what java does is not necessary unlike JS.
>>
>>61370881
JS is only necessary because it's the only thing that browsers support.
wasm will save us.
>>
>>61371088
>wasm will save us
no it wont

you still need js for web frontend
>>
>>61371111
>I don't know what wasm is
>>
>>61371088
>wasm
it will be used only for high performance requirements like games, js will be here forever
>>
>>61371198
I will write whatever supports wasm soonest just to avoid writing javascript.
>>
File: ss-2017-07-14-16-27-32.png (139KB, 724x450px) Image search: [Google]
ss-2017-07-14-16-27-32.png
139KB, 724x450px
>>61371119
>>
whats the different between a promise and doing something like this?

   $.ajax({
url: "whatever.php",
method: "POST",
data: $("#form_id_here").serialize(),
success: function () {
//do something
},
error: function () {
//do something
}
});
>>
>>61371289
As long as I can access the DOM, I will have no need to write any Javascript.
>>
>>61371296
try doing 10 subsequent requests inside the success callback that depend on the previous ones
>>
>>61371296
read up on callbacks vs promises
promises allow a nicer syntax but async/await blows both out of the water
>>
>>61369236
lol js const. Pass that variable to another function, and voila it's mutable again.
>>
>>61371349
>I don't understand what a reference type is
>>
>>61371329
I don't need to write any js even today. I use typescript exclusively

>>61371349
u wot
>>
>>61367233
Depends on the usecases, people here however are based autistics who will scold you for talking about the wrong hugbox.
>>
>>61371374
exactly
>>
>>61371331

how does a promise "fix" this then?
>>
>>61371374
This is unironically what many of my Node.js scripts look like.

Although I'll break it up into separate functions when it becomes too crazy.
>>
>>61371389
>>61371414
I fucked up, it's nearly 3am here, sorry guise
>>
>>61370066
which forced 4chan to install recaptcha.
>>
>>61371410
you can use .then() on promises to avoid callback hell or combine them all into a single one with Promise.all()
>>
>>61371360
it works in other fucking languages, why in javascript it's always a snowflake?
>>
>>61371519
Use final in Java and get zero surprises. Like I said, you don't understand reference types.
>>
>>61369838
Dunno about JS but Linq is slower i'm fairly sure.

I still use it though
>>
>>61371486

isnt the "then()" function just like calling another async function? i dont see the difference
>>
>>61371561
it isn't different functionally but it makes the code easier to read, write and reason about

http.get('https://api.github.com/repos/nodejs/node', function(response) {
console.log(response)
http.get('https://api.github.com/repos/nodejs/http2', function(response) {
console.log(response)
http.get('https://api.github.com/repos/nodejs/docker-node', function(response) {
console.log(response)
})
})
})


vs

var response = await http.get('https://api.github.com/repos/nodejs/node')
console.log(response)
var response = await http.get('https://api.github.com/repos/nodejs/http2')
console.log(response)
var response = await http.get('https://api.github.com/repos/nodejs/docker-node')
console.log(response)
>>
>>61367233
JS can be ok if used properly. Usually it isn't.
>>
>>61372906
This can be said about most programming languages.

With ES7 Javascript has become ok and it's legitimately better than all of the other popular dynamically typed languages. It's only beaten by niche dynamically typed languages (Racket, Smalltalk, Common Lisp etc.) and most statically typed languages.
>>
>>61371537
no I don't understand why it is called const.
>>
>>61373343
Because the object reference is constant.
It's a
Obj *const obj;
not a
const Obj *obj
>>
File: Untitled.png (260KB, 842x485px) Image search: [Google]
Untitled.png
260KB, 842x485px
>>61367233
Your typical JavaScript developer.
>>
>>61373393
No idea what he's talking about
>>
File: 1500051300618.png (2MB, 1076x1328px) Image search: [Google]
1500051300618.png
2MB, 1076x1328px
>>61373404
>>
>>61373393
> Rails

Every single time. They ALWAYS come from the Ruby community.
>>
>>61373393
>>61373419
jesus fucking christ
This is why we need fascism
>>
>>61373379
what the point then?
>>
>>61373578
so you never accidentally reseat the reference I guess

Immutable types are much more useful
Thread posts: 118
Thread images: 7


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.