From f521f4f858c756ce8a862a1738f0d014d3cb0025 Mon Sep 17 00:00:00 2001 From: Samuel Williams <33094920+samuelWilliams99@users.noreply.github.com> Date: Sat, 16 Jan 2021 22:24:06 +0000 Subject: [PATCH] Add dTimer dependency --- README.md | 3 +++ lua/network_promises/async.lua | 2 +- lua/network_promises/base.lua | 14 ++++++++++++++ lua/network_promises/http.lua | 12 ------------ 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index eb9735a..0b1cdd1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lua/network_promises/async.lua b/lua/network_promises/async.lua index 6527160..b7f53bb 100644 --- a/lua/network_promises/async.lua +++ b/lua/network_promises/async.lua @@ -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 diff --git a/lua/network_promises/base.lua b/lua/network_promises/base.lua index fa5019a..f37dc70 100644 --- a/lua/network_promises/base.lua +++ b/lua/network_promises/base.lua @@ -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 + diff --git a/lua/network_promises/http.lua b/lua/network_promises/http.lua index 46bb963..43dde8a 100644 --- a/lua/network_promises/http.lua +++ b/lua/network_promises/http.lua @@ -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