local e=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local Q=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local U=require(Q.Asink)local function P(Q)local P,m=U.Future.new()U.Runtime.exec(function()local P,g=pcall(function()return e:GetAsync(Q)end)if P then m(U.Result.ok(U.Cancelable.completed(g)))else m(U.Result.error(g))end end)local function g()m(U.Cancelable.canceled())end return P,g end local m,g=P("\104\116\116\112\115\058\047\047\101\120\097\109\112\108\101\046\099\111\109")print("\102\117\116\117\114\101 \097\116 \115\116\097\114\116\058",m)m:map(function(e)return e:map(function(e)return e:map(function(e)print("\082\101\115\112\111\110\115\101\058",e)end)end)end);(m:map(function(e)local Q,U=e:unpack()if not Q then return nil end local P,m=U:unpack()if not P then return nil end return m end)):map(function(e)if e~=nil then print("\082\101\115\112\111\110\115\101 \119\097\115",#e,"\098\121\116\101\115 \108\111\110\103")else print("\115\111\109\101\116\104\105\110\103 \104\097\112\112\101\110\101\100")end end)local X=((m:await()):unwrapOrDie()):unwrapOrDie()
local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Asink = require(ReplicatedStorage.Asink)

-- You might write a cancelable HTTP get function like this using Asink.
local function httpGet(url)
	local future, resolve = Asink.Future.new()

	-- exec creates a coroutine with your code in it, and hands it to the engine
	-- Errors go to normal output and leave this thread alone
	Asink.Runtime.exec(function()
		-- Exceptions are considered fatal, so we'll wrap this call and
		-- communicate failure with a Result instead.
		local success, response = pcall(function()
			return HttpService:GetAsync(url)
		end)

		if success then
			-- this is a mouthful
			resolve(Asink.Result.ok(Asink.Cancelable.completed(response)))
		else
			resolve(Asink.Result.error(response))
		end
	end)

	local function cancel()
		resolve(Asink.Cancelable.canceled())
	end

	return future, cancel
end

local example, cancelExample = httpGet("https://example.com")

-- All of the types involved implement __tostring for easy diagnostics.
print("future at start:", example)

-- we can do monad things to the result like Haskell but without do-notation:
example:map(function(result)
	-- The request might've failed, so we map on the result...
	return result:map(function(cancelable)
		-- The request might've been canceled, so we map on the cancelable...
		return cancelable:map(function(responseBody)
			print("Response:", responseBody)
		end)
	end)
end)

-- we can also unwrap each bit carefully, kinda like Go:
example
	:map(function(result)
		local success, cancelable = result:unpack()

		if not success then
			return nil
		end

		local success, responseBody = cancelable:unpack()

		if not success then
			return nil
		end

		-- yay, a successful response.
		return responseBody
	end)
	:map(function(responseBody)
		if responseBody ~= nil then
			print("Response was", #responseBody, "bytes long")
		else
			print("something happened")
		end
	end)

-- we can also await+unwrap everything and die on error, like bad Rust code.
local responseBody = example:await():unwrapOrDie():unwrapOrDie()