Httprequest.sendasync lacks class scope

I’ve been trying to write some class code, and this has been driving me up the wall trying to find a workaround for the last few days. Here’s some basic agent code to outline the problem:

`const URL = ""

class myClass
    val = 0
    function processResponse(incomingDataTable) 
        server.log("Code: " + incomingDataTable.statuscode + ". Message: " + incomingDataTable.body)
        this.val = 200 // BOOM.
    function log(logMessage) 
        local extraHeaders = {}
        local request = http.get(URL, extraHeaders)
        this.val = 100
local x = myClass()
x.log("Hello, World!")
server.log("object val = " + x.val)`

Running the above results in (somewhat edited to remove tagging):

[Agent]	object val = 100
[Agent]	Code: 200. Message: [...truncated...]
[Agent]	ERROR: the index 'val' does not exist
[Agent]	ERROR:   at processResponse:10

It seems to me that the sendasync() method needs to be sending a hidden first scope parameter (ie.- this), but it isn’t. Is there a way around this I’m not figuring out?

When you pass the handler processResponse to request.sendasync() you also need to pass the context that it will be called in, otherwise it will assume the global context. Because val is only visible inside the scope of myClass it will trigger a runtime error. There are a couple of ways of ensuring that processResponse is called with the right context. The easiest is to use


This link discusses the idea of contexts in more detail:

PS. I have no idea why the link is …/bindenvy Was that a little joke?

GAH! You know, I was pouring over the docs and I knew it had to be bindenv(), but I should have read the debounce example in the bindenv() and callbacks section most closely. That’s what I get for a week with lack of sleep. You totally nailed it. Thanks!

Joke’s on me, I WAS getting envious that it wasn’t working right… :stuck_out_tongue: