Diff

net/adns.lua @ 10614:431511e190bc

net.adns: Add :lookup_promise() method
author Matthew Wild <mwild1@gmail.com>
date Fri, 24 Jan 2020 13:50:02 +0000
parent 10114:2f7628804db6
child 10966:97de279ca01a
line wrap: on
line diff
--- a/net/adns.lua	Fri Jan 24 13:49:33 2020 +0000
+++ b/net/adns.lua	Fri Jan 24 13:50:02 2020 +0000
@@ -8,6 +8,7 @@
 
 local server = require "net.server";
 local new_resolver = require "net.dns".resolver;
+local promise = require "util.promise";
 
 local log = require "util.logger".init("adns");
 
@@ -91,6 +92,18 @@
 			end)(resolver:peek(qname, qtype, qclass));
 end
 
+function async_resolver_methods:lookup_promise(qname, qtype, qclass)
+	return promise.new(function (resolve, reject)
+		local function handler(answer)
+			if not answer then
+				return reject();
+			end
+			resolve(answer);
+		end
+		self:lookup(handler, qname, qtype, qclass);
+	end);
+end
+
 function query_methods:cancel(call_handler, reason) -- luacheck: ignore 212/reason
 	log("warn", "Cancelling DNS lookup for %s", self[4]);
 	self[1].cancel(self[2], self[3], self[4], self[5], call_handler);