function Rx.toPromise(R,d)local e=Maid.new()local B=CancelToken.new(function(R)e:GiveTask(R)if d then if d:IsCancelled()then R()else e:GiveTask(d.Cancelled:Connect(R))end end end)local C=Promise.new(function(d,C)if B:IsCancelled()then C()return end e:GiveTask(B.Cancelled:Connect(function()C()end))e:GiveTask(R:Subscribe(d,C,C))end)C:Finally(function()e:DoCleaning()end)return C end
--[=[
	Converts a promise to an observable.
	@param observable Observable<T>
	@param cancelToken CancelToken?
	@return Promise<T>
]=]
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
