Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Vei
i dont suppose ur an expert at javascript promises and love solving other ppls problems

Adbot
ADBOT LOVES YOU

Vei

hbag posted:

my poo poo's more python, lua, and a tiny amount of bash tbh
could probably give it a shot though
alright, newbie, *tucks icon away*, in order to truly be welcomed to this forum in which i am definitely an OG in, you must do my work for me

https://pastebin.com/JGs4kz4z

here is my code

im trying to call the 2 functions at the same time (so http requests go out at the same time), but function 2 shouldnt output its response unless function 1 is already finished running...


the problem is, i have no idea how Promises work, and apparently i've just made it so function 2 doesnt get called at all until function 1 is done*


*i feel bad already pls dont worry about my bugs**

**unless... haha....

Vei

hbag posted:

i'll give this a look when i can get around to remembering poo poo about js, no promises though
: ...................... )

Vei

cruft posted:

Would it be okay if I showed you how to do this without jQuery?

It's very simple.

! most definitely would be appreciated

Vei
now wait just a seco

Vei

cruft posted:

Here you go, fellow nerd

https://jsfiddle.net/36x9aonw/

I think of promises of sort of like how callbacks were used: when the thing is done, it executes the ".then()".

"async" makes it really easy to follow the flow of stuff, so you don't need ".then()" all over the place and your code isn't all ugly.

Let me know if you have questions!
holy poo poo thank you, gonna give this a shot! i haven't used "await" either before but this definitely looks like good stuff

and vanilla js is always ftw :^}

Adbot
ADBOT LOVES YOU

Vei

cruft posted:

Yeah, async/await are neato. I don't use them all the time because they're a bit more limited, but in a lot of cases they're just fine and make your code a lot easier to follow. Here's two ways to do the same thing:


JavaScript code:
function bar() {
  foo()
  .then((a) => {
    console.log(a)
  })
}
JavaScript code:
async function bar() {
  a = await foo()
  console.log(a)
}
Still getting the intuitive hang of it, but the code you provided is really great, thanks a ton man

got my 2 HTTP requests to start at the same time! which was my big goal



lowkey surprised that they are actually getting processed in parallel, even though i supposedly know that PHP creates new threads or w/e for each new process ...

man what a w00t day

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply