Entrance Events! Chat Gallery Search Everyone Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining the forum??
February 05, 2025 - @626.76 (what is this?)
Activity rating: Four Stars Posts & Arts: 92/1k.beats Unread Topics | Unread Replies | My Stuff | Random Topic | Recent Posts Start New Topic  Submit Art
News: :4u: Love is not possession  :4u: Super News: E-Zine #3 Accepting Entries!

+  MelonLand Forum
|-+  World Wild Web
| |-+  ✁ ∙ Web Crafting
| | |-+  ☔︎ ∙ I need Help!
| | | |-+  HELP: Guide on how to work with async functions


« previous next »
Pages: [1] Print
Author Topic: HELP: Guide on how to work with async functions  (Read 175 times)
edensgarden
Newbie ⚓︎
*


⛺︎ My Room

View Profile WWW

Joined 2024!
« on: December 27, 2024 @273.09 »

Hi! First post to the forums, hopefully I'm doing this right and I didn't miss someone making this exact post :skull:

So, I have an idea for a page I want to make on my own neocity, inspired by the way Melon's main site handles transitioning between iframe pages! That lift transition thing that I just think is so insanely cool. However, when I search up guides on how the whole async function works and how its structured, every single one leaves me more confused than knowledgeable, so I figured... why not ask here? See if someone here can explain it in a way that makes sense :ha:

The whole concept is that I want to do a character select kind of thing? There's the base image that I would overlay the shadow of a character over, and then whenever you click on an arrow left or right of the screen, the overlayed shadow would disappear, the animation moving from left to right or vice versa would happen, and then a new character shadow would appear.



I hope I wrote this in a way that makes sense, and thank you so much in advance if you do have any advice for this!
 :transport:

- Eden
Logged
Melooon
Hero Member ⚓︎
*****


So many stars!

⛺︎ My Room
SpaceHey: Friend Me!
StatusCafe: melon
iMood: Melonking
Itch.io: My Games
RSS: RSS

View Profile WWWArt

Thanks for being rad!a puppy for your travelsAlways My PalFirst 1000 Members!spring 2023!
« Reply #1 on: December 27, 2024 @789.95 »

That seems like a super cool idea! Its like the menu from SSX Tricky  :happy: Also also thanks for the kind words about my nav lift... so many people complained when it was first added  :tongue: (Though that was mostly due to an obnoxious beep I added) ~ I feel like alien bugs would live in this menu and they would eat glowing energy juice from the city pipes and city workers would have to come down and scare them away, and then one of them would uncover the ancient ruin, and it would turn out there had been an old city jail where a super villain was frozen and they would accidentally thaw him and then he would create a super bug army. ANYWAY that doesn't answer your question.

Sooo when a webpage loads, the code runs like a thread, from start to finish. Async functions are like adding extra smaller threads alongside the webpage loading, so you can weave a more complex program. They are especially useful if you want to add a time delay, or have something that waits for a while before happening.

To make a function run asynchronously, just add the word "async" before "function"; from that point on any time you run that function; it will, create its own thread, run that function in that thread, then the thread will disappear when the function finishes running.

Obvs this can become quite messy and complex, so you really need to think about what threads you are creating, why you need them to be in a thread, and how you will make sure to close your threads by finishing your function.

The benefit of course is that anything that happens in a thread is actually processed in a different part of your computers CPU/processor, meaning things that happen in one thread wont cause your main script to stop or slow down; and many threads can run simultaneously because your CPU has many processor cores. (In reality its a bit more complex, but you get the idea).

In the case of my elevator script, I used this bit of code:
Code
window.onload = async function () {
  await sleep(8000);
  window.open("/hidden/loading-error.html", "main");
};
function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

The code does the following things:
  • When the main window (your browser) finishes lowing the elevator page, it will trigger an event.
  • That event will create a new thread, and run a function in the thread.
  • That function will call another function called "sleep" and it will wait for it to return something.
  • When it awakes from its sleep, it will change the URL to the error page.

There is an extra function "sleep", that returns a Promise; a promise is exactly what it sounds like; it's a promise that something will happen in the future. So the sleep function is promising that it will do something in the future if you wait for it. Its a bit like if you sat at home waiting for your friend to call; doing that would be crazy if you didn't expect your friend to call, but if they had promised they would, then it would be quite reasonable to wait for them! Code feels the same way!  :4u:

NOW, in reality this is only part of my elevator script; my nav.html also has its own function running that is set to replace the contents of the main window before this script has a chance to finished waiting for 8000 milliseconds; if that happens, this page is obliterated by the new page; the promise becomes irrelevant, and the error page never loads.

The key things you need to remember are the "async function" creates a function that will exist in its own thread. "await" allows a function that is within a thread to pause and wait for something to be returned; And a "Promise" is a special kind of object you can return from a function to let another function know that it's worth waiting for.  :wizard:

I suppose it's also worth mentioning the idea of callbacks since they are very useful in asynchronous code! A callback is a function that your async function will call when it is finished processing or waiting. For example, say you had an async function that downloaded an image and you want to display that image on your webpage using a non async function. Your async function would download the image (this might be fast or slow depending on the person speed etc), then when it finished, it would call your non async function to say "Hey I have an image here, do your thing"!

That would like like this: (In reality this code is pointless since browser can load images anyway, but it's just an example!)
Code
function loadImage(imageData) {
  document.getElementById('myImageTag').src = imageData;
}
async function downloadImage(url, callback) {
  //Code that downloads the image etc
  //When done notify the callback
  callback(imageData);
}
//Run the program
downloadImage('bob.jpg', loadImage);

ANYWAY, hopefully that helps explain async!! Im not sure if its the correct thing for your situation, however it good to know anyway  :ozwomp:  I would also say don't be discouraged if you found this hard to follow, threaded coding is considered to be the most complex kind of coding and it can be very difficult to grasp even for experts! However with a bit of trial and error, and lots of patience you can do it!
« Last Edit: December 27, 2024 @795.02 by Melooon » Logged


everything lost will be recovered, when you drift into the arms of the undiscovered
edensgarden
Newbie ⚓︎
*


⛺︎ My Room

View Profile WWW

Joined 2024!
« Reply #2 on: January 18, 2025 @326.53 »

OH MY GOD I'm so sorry this is such a late response, got swept up in the real world for a moment, but thank you so much for this guide!!! This does manage to make a bit more sense so I really appreciate it!

I will work up the courage to try it... at some point!  :ha:

- Eden
Logged
Pages: [1] Print 
« previous next »
 

Vaguely similar topics! (3)

GUIDE: Melon's Simple X3D World Tutorial

Started by MelooonBoard ♺ ∙ Web Crafting Materials

Replies: 5
Views: 5059
Last post February 17, 2022 @637.75
by demonologi
How to use a forum?! - A guide for those who have forgotten/never knew!

Started by MelooonBoard ⛄︎ ∙ Forum Info & Questions

Replies: 2
Views: 4549
Last post April 22, 2023 @739.62
by Aloe
GUIDE: How to fix links on a frame site! - MK Frame-Link System

Started by MelooonBoard ✁ ∙ Web Crafting

Replies: 10
Views: 11424
Last post December 13, 2024 @220.02
by serenefork

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021 | Privacy Notice | ~ Send Feedback ~ Forum Guide | Rules | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi