sundar
sundar

Reputation: 1

What will this 3rd arg of functions will do where function definition will be there in lua?

I am looking for an explanation of the 3rd argument in uv.tcp_connect and uv.getaddrinfo. How to get the real function name or fun definition? At tcp_connect I saw this third argument as function(err).

  1. function(err) -error is string At getaddress has 3rd argument.

  2. function(res,err) Most of the places callback() or function () is calling.

  3. callback() - how to determine this callback is going to call which API ? I know it's all are callback but in my lua code difficult to find fun definition.

Upvotes: 0

Views: 64

Answers (1)

Piglet
Piglet

Reputation: 28950

I can only guess what you want to know.

uv.tcp_connect(tcp, host, port, callback) callback is -function(err)

This line tells you that the function uv.tcp_connect has four parameters. tcp, host, port and callback.

callback is a function value with one parameter err.

So you would typically do something like this (assuming err is a str

local myCallback = function (err) print("The error is: " .. err) end
uv.tcp_connect(myTcp, myHost, myPort, myCallback)

Or using an anonymous function:

uv.tcp_connect(myTcp, myHost, myPort, function (err) print("The error is: "..err) end)

At some point the program will call your callback and provide the arguments according to the parameter list.

Upvotes: 2

Related Questions