Diff

util/promise.lua @ 9517:b1c6ede17592

util.promise: Add promise.try()
author Matthew Wild <mwild1@gmail.com>
date Thu, 18 Oct 2018 18:03:31 +0100
parent 9515:2571c65b972f
child 9546:c93496785022
line wrap: on
line diff
--- a/util/promise.lua	Thu Oct 18 18:00:54 2018 +0100
+++ b/util/promise.lua	Thu Oct 18 18:03:31 2018 +0100
@@ -51,6 +51,7 @@
 		if resolved then return; end
 		resolved = true;
 		if is_promise(e) then
+			print ("WOAH") assert(false)
 			e:next(new_resolve_functions(p));
 		elseif promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then
 			p.reason = e;
@@ -117,6 +118,10 @@
 	end);
 end
 
+local function try(f)
+	return resolve():next(function () return f(); end);
+end
+
 function promise_methods:next(on_fulfilled, on_rejected)
 	return new(function (resolve, reject) --luacheck: ignore 431/resolve 431/reject
 		self:_next(
@@ -142,4 +147,5 @@
 	reject = reject;
 	all = all;
 	race = race;
+	try = try;
 }