coroutine.wrap
Örnek
Kodun yürütülmesine devam etmek için sunucu yanıtını beklerken standart bir HTTP isteği gerçekleştirir. Kodun okunabilirliğini artırmak ve geri arama (callback) fonksiyonlarını kullanmaktan kaçınmak için faydalıdır.
local function HTTPRequest( url, headers )
local running = coroutine.running()
local function onSuccess( body, length, header, code )
coroutine.resume( running, true, body )
end
local function onFailure( err )
coroutine.resume( running, false, err )
end
http.Fetch( url, onSuccess, onFailure, headers )
return coroutine.yield()
end
coroutine.wrap( function()
local state, response = HTTPRequest( "https://www.google.com/" )
if state then
-- Başarılı!
print( "HTTP gövdesi:" .. response )
else
-- Hata(?)
print( "HTTP isteği hatası: " .. response )
end
-- İstek tamamlandıktan sonra gerçekleştirilecek önemli kod..
end )()
Çıktı: İstek başarılıysa HTTP sayfasının gövdesi, aksi takdirde başarısızlık durumunda hata kodu.
Tüm sunucular · GmodTR Launcher · Rehber · GMod Wiki · GmodTR nedir? · Sunucu ekle · Discord