Add dTimer dependency

This commit is contained in:
Samuel Williams 2021-01-16 22:24:06 +00:00
parent 2c63769c19
commit f521f4f858
4 changed files with 18 additions and 13 deletions

View File

@ -2,6 +2,9 @@
# cfc_network_promises
Network-based promise library
## Requirements
- [cfc_detached_timer](https://github.com/CFC-Servers/cfc_detached_timer)
## Async + Await
This project implements a C#/javascript style async and await functionality.
Similarly to Javascript, this is built on top of promises.

View File

@ -10,7 +10,7 @@ end
-- Delay to escape any enclosing pcalls
local function delayPromise( prom, method, ... )
local data = { ... }
timer.Simple( 0, function()
dTimer.Simple( 0, function()
prom[method]( prom, unpack( data ) )
end )
end

View File

@ -1,3 +1,4 @@
require( "cfc_detached_timer" )
local deferred = include( "network_promises/include/deferred.lua" )
promise = deferred -- Create global promise variable for other scripts
@ -8,3 +9,16 @@ include( "network_promises/http.lua" )
include( "network_promises/net.lua" )
include( "network_promises/async.lua" )
include( "network_promises/util.lua" )
-- Returns a promise that resolves after t seconds
-- fail : Should the promise reject after timeout
function NP.timeout( t, fail )
local prom = promise.new()
local method = fail and prom.reject or prom.resolve
dTimer.Simple( t, function()
method( prom, "Timeout" )
end )
return prom
end

View File

@ -1,17 +1,5 @@
NP.http = {}
-- Returns a promise that resolves after t seconds
-- fail : Should the promise reject after timeout
function NP.timeout( t, fail )
local prom = promise.new()
local method = fail and prom.reject or prom.resolve
timer.Simple( t, function()
method( prom, "Timeout" )
end )
return prom
end
local function responseSuccess( prom, body, status, headers )
local isSuccessStatus = math.floor( status / 100 ) == 2