Sub Process_Globals
' to hold our TimeOut object
Public myTimeOut As Object
End Sub
' A new fetch method with a Timeout
Sub FetchTimeout(url As String, options As BANanoFetchOptions, timeout As Long) As BANanoPromise
Dim prom As BANanoPromise = BANano.PromiseRace(Array(TimeOutAfter(timeout), Fetch3000(url, options)))
Return prom
End Sub
#Region Fetch Promise
Sub Fetch3000(url As String, options As BANanoFetchOptions) As BANanoPromise
Dim prom As BANanoPromise
' with ...Wait because we use a Sleep method in it to fake the delay
prom.CallSub(Me, "FetchDelayed3000Wait", Array(url, options))
Return prom
End Sub
Sub FetchDelayed3000Wait(url As String, options As BANanoFetchOptions)
Dim response As BANanoFetchResponse
Dim error As Object
Dim Fetch As BANanoFetch
Fetch.Initialize(url, options)
Fetch.ThenWait(response) ' wait because of the sleep method
' clear the TimeOut
BANano.Window.ClearTimeout(myTimeOut)
' fake 3 seconds delay
Sleep(3000)
If response.OK Then
BANano.ReturnThen(response)
Else ' some other error
BANano.ReturnElse("Whoops, something else went wrong (file did not exist?)...")
End If
Fetch.Else(error)
' clear the TimeOut
BANano.Window.ClearTimeout(myTimeOut)
' return the error
BANano.ReturnElse(error)
Fetch.end
End Sub
#end Region
#Region TimeOut Promise and Helpers
Sub TimeOutAfter(timeout As Long) As BANanoPromise
Dim prom As BANanoPromise
prom.CallSub(Me, "DoTimeOutAfter", Array(timeout))
Return prom
End Sub
Sub DoTimeOutAfter(timeout As Long)
myTimeOut = BANano.Window.SetTimeout(BANano.CallBack(Me, "DoTimeout", Null),timeout)
End Sub
Sub DoTimeout() 'ignore
BANano.ReturnElse("Request Time-out")
End Sub
#End Region