mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 23:50:03 +00:00
parent
65daff4777
commit
c0626c0baf
4 changed files with 27 additions and 2 deletions
|
@ -5,6 +5,8 @@
|
||||||
- Fix issue with Promise.race/all always cancelling instead of only cancelling if the Promise has no other consumers
|
- Fix issue with Promise.race/all always cancelling instead of only cancelling if the Promise has no other consumers
|
||||||
- Make error checking more robust across many methods.
|
- Make error checking more robust across many methods.
|
||||||
- Promise.Status members are now strings instead of symbols, and indexing a non-existent value will error.
|
- Promise.Status members are now strings instead of symbols, and indexing a non-existent value will error.
|
||||||
|
- Improve stack traces
|
||||||
|
- Promise.promisify will now turn errors into rejections even if they occur after a yield.
|
||||||
|
|
||||||
# 2.4.0
|
# 2.4.0
|
||||||
|
|
||||||
|
|
|
@ -129,6 +129,8 @@ docs:
|
||||||
desc: |
|
desc: |
|
||||||
Wraps a function that yields into one that returns a Promise.
|
Wraps a function that yields into one that returns a Promise.
|
||||||
|
|
||||||
|
Any errors that occur while executing the function will be turned into rejections.
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local sleep = Promise.promisify(wait)
|
local sleep = Promise.promisify(wait)
|
||||||
|
|
||||||
|
|
10
lib/init.lua
10
lib/init.lua
|
@ -368,10 +368,16 @@ end
|
||||||
]]
|
]]
|
||||||
function Promise.promisify(callback)
|
function Promise.promisify(callback)
|
||||||
return function(...)
|
return function(...)
|
||||||
|
local traceback = debug.traceback()
|
||||||
local length, values = pack(...)
|
local length, values = pack(...)
|
||||||
return Promise.new(function(resolve)
|
return Promise.new(function(resolve, reject)
|
||||||
coroutine.wrap(function()
|
coroutine.wrap(function()
|
||||||
resolve(callback(unpack(values, 1, length)))
|
local ok, resultLength, resultValues = packResult(pcall(callback, unpack(values, 1, length)))
|
||||||
|
if ok then
|
||||||
|
resolve(unpack(resultValues, 1, resultLength))
|
||||||
|
else
|
||||||
|
reject((resultValues[1] or "error") .. "\n" .. traceback)
|
||||||
|
end
|
||||||
end)()
|
end)()
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
|
@ -624,6 +624,21 @@ return function()
|
||||||
expect(status).to.equal(Promise.Status.Resolved)
|
expect(status).to.equal(Promise.Status.Resolved)
|
||||||
expect(result).to.equal(2)
|
expect(result).to.equal(2)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("should catch errors after a yield", function()
|
||||||
|
local bindable = Instance.new("BindableEvent")
|
||||||
|
local test = Promise.promisify(function ()
|
||||||
|
bindable.Event:Wait()
|
||||||
|
error('errortext')
|
||||||
|
end)
|
||||||
|
|
||||||
|
local promise = test()
|
||||||
|
|
||||||
|
expect(promise:getStatus()).to.equal(Promise.Status.Started)
|
||||||
|
bindable:Fire()
|
||||||
|
expect(promise:getStatus()).to.equal(Promise.Status.Rejected)
|
||||||
|
expect(promise._values[1]:find("errortext")).to.be.ok()
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("Promise.tap", function()
|
describe("Promise.tap", function()
|
||||||
|
|
Loading…
Reference in a new issue