keyword async / await

Propose new features and changes
bourdin07
Posts: 36
Joined: 23 Jun 2019, 12:57

keyword async / await

29 Jul 2019, 10:47

I wish to add async method in a class

Code: Select all

class MyClass
{
    async myMethodAsync()
    {
        ; awesome job
       
    }
}

MyClass.myMethodAsync()

; do other job without waiting MyClass.myMethodAsync() ...
irral
Posts: 6
Joined: 14 May 2019, 02:38

Re: keyword async / await

29 Jul 2019, 14:59

For v2

Code: Select all

any_method() ;
{
}
; ...
; ... some code
SetTimer Func("any_method"), -1 ; it calls async
; ... 
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: keyword async / await

03 Aug 2019, 02:12

Before we can have an async keyword, the language must be capable of parallel execution within a single instance of the interpreter. Realistically, I do not see this ever happening. For more details you may refer to previous topics about multi-threading.

Using SetTimer is different to a direct function call in that
a) it starts a new quasi-thread; and
b) the function is called when the script next checks its timers, which could be after executing an unpredictable number of lines below the SetTimer call; but
c) it might be postponed if the current thread does not permit interruption by timers.

The script is still unable to do anything else while waiting for any_method to return. It might be similar to moving the function call downward a random number of lines.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: keyword async / await

03 Aug 2019, 05:42

I think async only needs to start a pseudo thread like SetTimer does - because its async not paralel
Recommends AHK Studio
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: keyword async / await

09 Aug 2019, 21:01

A pseudo-thread is merely a bundle of values for built-in variables, saved and restored at specific times. If one disregards the built-in variables, starting a new pseudo-thread means nothing. If you want to start a new pseudo-thread immediately, you can use CallbackCreate/DllCall. It is beside the point of async.

I admit, I'd forgotten what async actually does, having read about it but never used it. My understanding is this: An async function executes synchronously up until it awaits some other function, at which point control is returned to its caller while the other function's task (not the function itself) is executed asynchronously. The caller receives a Promise (JS) or Task (C#) object (which might be handled transparently if the caller is using await).

An async function without await is executed synchronously, so ultimately every async function depends on a Promise or Task that someone has implemented directly, without async (perhaps built-in, but not necessarily). There is probably nothing that you can do with async/await that can't already be done with callbacks (especially in v2.0, having closures). I suppose that it would be possible to implement async/await with just a source code preprocessor utilizing closures.

In other languages, I believe async and await were added to simplify the use of Promises or Tasks. We don't have those in the first place.
bourdin07
Posts: 36
Joined: 23 Jun 2019, 12:57

Re: keyword async / await

12 Aug 2019, 08:37

I don't know if it can help but, I wrote a program in Autohotokey to do XHR asynchronously.

May be it is possible to do the same way with autohotkey and "user" function

Code: Select all

class Program
{
    Main(args)
    {
        this.tt := "k"
        req := ComObjCreate("Msxml2.XMLHTTP")
        ; Open a request with async enabled.
        req.open("GET", "https://autohotkey.com/download/1.1/version.txt", true)
        ; Set our callback function [requires v1.1.17+].
        ; req.onreadystatechange := Func("_AHK.Program.Ready").Bind("", req)
        req.onreadystatechange := this.Ready.Bind(this, req)
        req.send()
        
        ; do other dirty job without waiting...
        ; ...
    }

    Ready(req)
    {
        if (req.readyState != 4)  ; Not done yet.
            print(req.readyState)
            , return

        if (req.status == 200) ; OK.
            print(this)
            , print("Latest AutoHotkey version: " req.responseText)
        else
            print("Status " req.status)

        ExitApp
    }
}

Return to “Wish List”

Who is online

Users browsing this forum: burque505 and 53 guests