-
Notifications
You must be signed in to change notification settings - Fork 0
/
miku.lua
37 lines (31 loc) · 1.05 KB
/
miku.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
local hatsuneModule = require("hatsune")
local await, async, hatsune, miku = hatsuneModule.await, hatsuneModule.async, hatsuneModule.hatsune, hatsuneModule.miku
local awaitSafe, throw = hatsuneModule.awaitSafe, hatsuneModule.throw
local scheduler = hatsune()
-- this is an async function, which automatically returns a miku (future/promise) for you
local get = async(function(value)
sleep(1)
return value
end)
-- this is a longer form of the above, which also allows you to reject with a value rather than just throwing an error
local getMiku = function(value)
return miku(function(resolve, reject)
sleep(1)
resolve(value)
end)
end
local reject = async(function()
throw("rejected")
end)
-- longer form of the above
local rejectMiku = function()
return miku(function(resolve, reject)
reject("rejected")
end)
end
scheduler:run(function()
print(await(get("Hello, this is an example of hatsune!")))
getMiku("There are many ways to use this library, here are a few!"):done(print):await()
print(awaitSafe(rejectMiku()))
await(reject())
end)