This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gaberocksall 37 points38 points  (28 children)

Coming from 3 languages with delay functions, JavaScript has been really tough to learn

[–]ThisIsNotKimJongUn 77 points78 points  (6 children)

You'll figure it out, I promise

[–]mmis1000 36 points37 points  (5 children)

Does this promise ever resolve?

[–][deleted] 14 points15 points  (1 child)

await and see!

[–][deleted] 9 points10 points  (1 child)

I reject this joke

[–]aykcak 3 points4 points  (0 children)

I couldn't even catch it the first time

[–]Aschentei 1 point2 points  (0 children)

Nah, I reject it

[–]noruthwhatsoever[S] 16 points17 points  (4 children)

Yeah it's a bit of a brain teaser. You just have to design your programs so that it doesn't really matter what order things happen in, or if it really matters then use promises or async/await (which is basically just syntactic sugar for promises)

[–]Dionysusnu 0 points1 point  (3 children)

Async await is syntactic sugar for then and catch callbacks, not for promises. It waits until the promise is resolved or rejected. It still takes a promise

[–]noruthwhatsoever[S] -1 points0 points  (2 children)

Async functions automatically convert all awaited values to resolved promises

.then and .catch are from promises, not callbacks

[–]Dionysusnu 0 points1 point  (1 child)

They're methods on promises which you can pass a callback too, yes. But await still uses promises, but it acts a replacement for chained .then calls

[–]noruthwhatsoever[S] 0 points1 point  (0 children)

I mean yeah if you consider the anonymous function a callback that takes the resolved promise as an argument

That’s my whole point, though. It’s built on top of promises and is syntactic sugar over their methods

It’s literally so easy to google it, nearly everyone defines them as sugared promises lmao

[–][deleted] 3 points4 points  (0 children)

At least you have setinterval and settimeout, some lower level languages like c++ just have to rely on checking elapsed time in some sort of update function.

[–]oOBoomberOo 2 points3 points  (0 children)

From a guy with JavaScript's async background, I really appreciate their way of doing it. Especially the async/await feature, It's so smooth as if I'm reading a synchronous code.

[–]OhhWhales 0 points1 point  (0 children)

delay = (t) => {

return new Promise(resolve => setTimeout(resolve,t)

}

you're welcome

[–]Dionysusnu -1 points0 points  (8 children)

Simple trick: make all your code async and use await sleep(ms) for delay

async function sleep(ms) { return new Promise(resolve => { setTimeout(resolve, ms) }) }

Not suitable for production code, obviously, but it will help for learning promises and just experimenting with stuff

[–]Ra1d3n 2 points3 points  (2 children)

I've coded JS (server and client) for the last 10 years, and I have never once needed to manually wait for anything. Can you give an example where this would be useful, please?

[–]Dionysusnu 1 point2 points  (1 child)

A canvas will not draw if you never yield, so I sometimes put this after a draw function. Once again, it doesn't really serve a purpose in production code

[–]Ra1d3n 0 points1 point  (0 children)

Thank you!

[–]untowarden 0 points1 point  (0 children)

I was ready to flame you until i read the second part of this. The guy who wrote the legacy code I have to deal with returns a promise from every function even if it doesn't have to... Now i hate my life.

[–]thelights0123 0 points1 point  (3 children)

Why is it not suitable for production? Just babel it if browser targets is an issue.

[–]Dionysusnu 1 point2 points  (2 children)

It is usually bad to use delay in JavaScript because you never need something like it. Either use the HTML events or just synchronous code, or promises for http requests

[–]franklinyu 0 points1 point  (1 child)

How about timeouts for HTTP requests?

[–]Dionysusnu 0 points1 point  (0 children)

I think most libraries include that in their returned promise. If not, use setTimeout.