aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--spec/util_async_spec.lua14
-rw-r--r--util/async.lua6
2 files changed, 19 insertions, 1 deletions
diff --git a/spec/util_async_spec.lua b/spec/util_async_spec.lua
index 58d54487..ac71e755 100644
--- a/spec/util_async_spec.lua
+++ b/spec/util_async_spec.lua
@@ -579,4 +579,18 @@ describe("util.async", function()
assert.spy(r.watchers.ready).was_not.called();
end);
end);
+
+ describe("#ready()", function ()
+ it("should return false outside an async context", function ()
+ assert.falsy(async.ready());
+ end);
+ it("should return true inside an async context", function ()
+ local r = new(function ()
+ assert.truthy(async.ready());
+ end);
+ r:run(true);
+ assert.spy(r.func).was.called();
+ assert.spy(r.watchers.error).was_not.called();
+ end);
+ end);
end);
diff --git a/util/async.lua b/util/async.lua
index 374a3bdf..1930d1a9 100644
--- a/util/async.lua
+++ b/util/async.lua
@@ -215,4 +215,8 @@ function runner_mt:log(level, fmt, ...)
return log(level, "[runner %s] "..fmt, self.id, ...);
end
-return { waiter = waiter, guarder = guarder, runner = runner };
+local function ready()
+ return pcall(checkthread);
+end
+
+return { ready = ready, waiter = waiter, guarder = guarder, runner = runner };