lua | ||
LICENSE | ||
README.md |
cfc_network_promises
Network-based promise library
Usage
-
NP.net.send( name, ... )
Send a net message onname
containing the varargs...
, no need for anynet.WriteString
or likewise. Returns a promise resolving or rejecting with whatever data returned viaNP.net.receive()
Note: This function sends more than just the varargs, only receive this withNP.net.receive()
-
NP.net.receive( name, func )
Receive a net message onname
callingfunc
, where func is of the form:function( callingPly, ... )
The...
will be whatever they were in the correspondingNP.net.send()
Note: Both send and receive will check if the name is already pooled, and if not, will add it as a network string (NP.net.send
will still error if called on client using an unpooled name ) -
NP.http.post( url, data, timeout )
Post data to aurl
, with an optionaltimeout
( defaulting to 5 seconds ). Returns a promise resolving in the form:function( statusCode, jsonData, headers )
where jsonData is parsed from the post body, or rejecting in the form:function ( errorStr )
Note: The url must return in JSON, if the JSON cannot be parsed withutil.JSONToTable()
then the promise will reject. -
NP.http.fetch( url, timeout )
Identical toNP.http.post()
but fetches instead.
Example
Server side:
-- "getData" automatically pooled by this function.
-- Receive on getServerStatus, return the result of fetching the scripting url
NP.net.receive( "getData", function( ply )
return NP.http.fetch( "https://my.website.com/getData" )
end )
Client side:
-- Send net message, print result or error
NP.net.send( "getData" ):next( function( status, data )
-- Success
print( status ) -- Hopefully 200
PrintTable( data ) -- Some data
end, function( err )
-- Failure
print( "Error: ", err )
end )
Credit
The deferred library (lua/network_promises/include/deferred.lua
) was written by zserge, here, and modified for CFC to support varargs in promise resolves and rejects.