function Rx.toPromise(H,S)local h=Maid.new()local i=CancelToken.new(function(H)h:GiveTask(H)if S then if S:IsCancelled()then H()else h:GiveTask(S.Cancelled:Connect(H))end end end)local D=Promise.new(function(S,D)if i:IsCancelled()then D()return end h:GiveTask(i.Cancelled:Connect(function()D()end))h:GiveTask(H:Subscribe(S,D,D))end)D:Finally(function()h:DoCleaning()end)return D end

function Rx.toPromise(observable, cancelToken)
	local maid = Maid.new()

	local newCancelToken = CancelToken.new(function(cancel)
		maid:GiveTask(cancel)
		if cancelToken then
			if cancelToken:IsCancelled() then
				cancel()
			else
				maid:GiveTask(cancelToken.Cancelled:Connect(cancel))
			end
		end
	end)

	local promise = Promise.new(function(resolve, reject)
		if newCancelToken:IsCancelled() then
			reject()
			return
		end

		maid:GiveTask(newCancelToken.Cancelled:Connect(function()
			reject()
		end))

		maid:GiveTask(observable:Subscribe(resolve, reject, reject))
	end)

	promise:Finally(function()
		maid:DoCleaning()
	end)

	return promise
end
