~singpolyma/em_promise.rb

a91c67edf393000f2e2d2358a7cc3c07016d3b75 — Stephen Paul Weber a month ago 3eb8964 master 0.0.2
Allow users to catch only some errors

This implements the common pattern:

promise.catch do |e|
	if SomeError === e
		# handle
	else
		EMPromise.reject(e)
	end
end

It also allows for this usage for more granular catching:

promise.catch_only(filter: ->(e) { e == :done }) do
	"all done"
end
1 files changed, 10 insertions(+), 0 deletions(-)

M lib/em_promise.rb
M lib/em_promise.rb => lib/em_promise.rb +10 -0
@@ 34,6 34,16 @@ class EMPromise < Promise
		new.tap { |promise| promise.reject(e) }
	end

	def catch_only(*klasses, filter: ->(*) { false })
		catch do |e|
			if klasses.find { |klass| e.is_a?(klass) } || filter[e]
				yield e
			else
				EMPromise.reject(e)
			end
		end
	end

	def self.all(enumerable)
		super(enumerable.map { |input|
			if input.respond_to?(:promise)