Simple Coroutine Style Yields in JavaScript/TypeScript

Hey Folks!

If you are like me you have likely spent most of your time in a coding language that is not JavaScript (JS) or TypeScript (TS). Maybe you come from the beautifully brilliant world of C#. If you do then you’ve likely got an understanding of the gorgeous simplicity and power of a coroutine.

As a refresher or perhaps a primer, what is a coroutine? Put at its simplest a coroutine is an asynchronous function. They allow you to multitask instead of simply completing each function in a sequence. If you were hoping for anything more technical than this I don’t think I’m the person to turn to.

So you’ve jumped into JS then and you find yourself really needing a waitForSeconds style function like you’ve been using in Unity or the equivalent in Unreal.

You are probably thinking at this point:

Technically they don’t exist. But you don’t need to jump out a window quite yet.

It turns out there is a system that’ll achieve this goal and it is known as “Promises”. If you’d like to go down a path of suffering I suggest you google JavaScript Promises and try to find an example that is human readable.

I’m unclear why but apparently it is illegal to make something that is easy to understand and even easier to implement. Everyone has nested brackets or otherwise hideous loops that take a phenomenal amount of time to grasp.

And don’t even get me started on all the thens and catches. I get that eventually you will want to know these things but come on folks. For game development most of that fluff is just not helpful.

Now that I’ve given you an appropriately long intro akin to a baking recipe blog, I’ll drop the easy out!

First place the code below outside of your class, for convenience I’d suggest placing it above the class.

export function waitforseconds(seconds: number): Promise<void>
    return new Promise<void>(resolve =>
        setTimeout(() => { resolve(); }, seconds * 1000);

Once that is done you can now add “waitforseconds” awaits in any async function as shown here:

    async codeExample() : Promise<void>
        for (var i = 0; i < 5; i++)
            cc.log("You'd put any code you wanted to happen before the next iteration here.");
            await waitforseconds(1.1); //And this will wait for 1.1 seconds.

You’ll notice two major differences to this logic over other in class functions. Firstly it starts with “async” before the function name. This is mandatory to make “await” work. Next we have the function type of “Promise<void>”. In this case that is because we aren’t returning anything.

And that’s it! Turns out the above code works both for TypeScript and JavaScript without any formatting changes.

It is worth noting that if you aren’t sure how to call the above codeExample you can do so like you would any other function. A simple “this.codeExample();” will work brilliantly.

You can use as many awaits as you want in just about any place you want. The function will wait for the duration to pass before it continues on with the logic but it will not hang your program while it does so.

If the function you are making needs to be universal like the await itself (or is just not class specific) you simply need to add “function” after “async” and it’ll work.

See how easy this was? No PHD in hieroglyphics needed. At worst the actual promise itself is a bit hideous but you don’t need to read it beyond grasping the basic fundamentals. It waits x seconds and then releases the asynchronous function to continue.

Finally, feel free to rename it “waitForSeconds”. I’ve been meaning to do that but honestly at a certain point you just shrug and say “You are the monster I created and I love you.”

[If you like this or have any questions feel free to ping me @oafkad on twitter.]


The Internet: Vast Nothingness

Hey Folks!

  It is fascinating how the internet is both unbelievably enormous and yet simultaneously tiny. As wide as an ocean and as deep as a kiddy pool. According to Wikipedia, the indexed internet is around 14.5 billion web pages (as of 2015, even more now). This is a fraction of the total web pages on the internet. With the deep web accounting for 400 to 600 times more than that.

  And yet, with this almost completely impossible to grasp amount of content the vast majority of people have been funneled into a handful of websites. has a handy collection of the most searched websites of 2019. Major banks, major retailers, and a few google services make up the majority of the entries. Alongside this we see a focus on health and social networks.

  I’d argue those go hand in hand, since your health is probably poor if you frequent social networks. But I have nothing but my own distaste of SM to defend that belief with.

  Hundreds of billions of websites but most of them are unvisited by the vast majority of human beings. They live and die without anyone ever knowing and I find that fascinating. When I was little and the internet was budding I would visit different websites almost every day. I would scour the internet for the perfect Dragon Ball Z gifs to add to my collection. It was this wild world where each day promised new discoveries.

Another for the collection.

This was intentional.

That’s what I find most fascinating about the modern internet. The murder of this exploration format was done intentionally. As reported by The Vulture and at this point a billion other places (that you’ll never visit), Facebook and other SNs falsified their click-through rates. This caused advertisers to leave other services and flood SNs.

What results is all the competition dies out for lack of funds and all you are left with is a small handful of (arguably) garbage websites. Where once you might suggest that the free market could handle problems and self cultivate that opportunity is now gone.

At a certain point services become so big that they become the defacto option. Wealth and power both have a gravitational pull, once you’ve reached a singularity for either the capacity for anyone to escape it is physically zero.

This likely isn’t good.

I can’t quantify this easily. But I do think that this is a very bad situation for the internet to be in. This wild consolidation of viewership and communities will result in mostly negative outcomes. My logic is as follows.

When eyes are spread across dozens, or thousands, of forums, websites, and other services, this makes any coordinated attack to manipulate people costly and difficult. Once you consolidate everyone to a single, or small handful, of locations you can then consolidate your attack vectors as well.

This means that faking political, religious, or cultural views of a destructive nature becomes trivial. It is inexpensive and almost curates itself. Once you plant the seed it spreads like a disease. And since we’ve gathered everyone into densely packed herds online those diseases spread with wild efficiency.

This is probably permanent.

The problem with the situation is that the internet is largely seen as a free service. The majority of services that you enjoy are ad driven and this means that they must appeal to advertisers first, and you second. This also means that whoever has the largest population also has the largest draw for advertisers.

It creates an ouroboros style situation. Everything cycles in upon itself and the gorging continues endlessly.


The only possible way that I can see this ever improving is if paid services become more appealing to people. I’m not recommending people pay for YouTube but I’m going to use it as an example.

Most of the negative changes to YouTube have been because it is ad driven. In order to appeal to advertisers they have collected heavy amounts of data on all of their users and they’ve also had a heavy hand in moderating the content on the website.

This first point is what recently got them into enormous legal trouble. They were collecting tons of data on children. Now, if the majority of their revenue came from YouTube Red there would be no real incentive to collect data on users. Giving it away to third parties would create competitors for their information market. And if they don’t need advertisers to stay profitable it also eliminates the need to be heavy handed with moderation.

Users will watch what they enjoy, part of the revenue from their subscription will go to those people, and anyone not being watched will not be making money.

This is just one example, and not even a very good one if I’m being honest. YouTube and Google at large have likely been just as complicit in manipulating their actual efficacy as any other service. You don’t get as large as Google without doing some fairly awful things.

So that’s my little rant for the day. The internet as an ad driven service has, in my opinion, died. Everything is consolidated into a small handful of places, almost all advertising revenue is transferring through a small handful of companies, and at this point it is something of an auto-piloting downward spiral.


Hey Folks!

I Live!

I can’t believe it. I’ve been thinking about restarting the website for years. To basically take a bare bones theme and adjust it to fit all of my needs. At this point there is no real reason to buy or use any of the complicated multi themes that are floating out there.

Folks say they are making efficient themes but in my experience that just isn’t the case. Incredibly high use of JavaScript, tons of badly placed routines, and numerous calls being called over and over.

With the coming new year I’m going to do my best to be more active on here. There is no real benefit to me posting on other website. Popularity on social networks is nice, it does feed some kind of narcissism that I think we all suffer from on one level or another.

But do you know what the next level of narcissism is?

Pictured: Me in the morning.

Putting all my thoughts and content on my own website! That to me seems like the height of “Please god, look at me.” and you know what? I’m ok with that. If nothing else this’ll keep me busy and I think that can be handy when it feels rewarding.

So where from here?

Well first off I’ll be starting up YouTube videos again with the new year. I’m going to be setting all my old videos to unlisted. However they’ll still be available via playlists and finding them on the internet somewhere.

Additionally all my old posts are archived. I’ll be posting some of them again in the future, likely edited and improved. Over time I’m going to try and add everything I want on my website myself so that I don’t need to worry about the plugin owner updating and breaking my stuff.

Because I’ll be that owner. Which means the plugins will likely still break but it’ll be my fault at least.

There isn’t much else to say. The next few weeks and months will be about improving the look and feel of this place. Once I have it looking the way I like I’ll then move on to integrating a forum solution of some kind. I miss forums, I feel like they were the height of discourse on the internet.

Ever since we moved away from specialized forums to these larger hotpot social networks I think things have gotten more hostile. Nobody really knows anyone anymore and creating positive connections has become far too complicated.

That is a larger rant for another day however. For now I’m just fairly excited about this. It’ll be nice to have another hobby with largely instant feedback.