mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 23:50:03 +00:00
Add Promise.try
This commit is contained in:
parent
f6c3dd163a
commit
3052ec1474
4 changed files with 53 additions and 1 deletions
|
@ -7,6 +7,7 @@
|
||||||
- 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
|
- Improve stack traces
|
||||||
- Promise.promisify will now turn errors into rejections even if they occur after a yield.
|
- Promise.promisify will now turn errors into rejections even if they occur after a yield.
|
||||||
|
- Add Promise.try
|
||||||
|
|
||||||
# 2.4.0
|
# 2.4.0
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,7 @@ docs:
|
||||||
```
|
```
|
||||||
static: true
|
static: true
|
||||||
params:
|
params:
|
||||||
- name: function
|
- name: callback
|
||||||
type:
|
type:
|
||||||
kind: function
|
kind: function
|
||||||
params: "...: ...any?"
|
params: "...: ...any?"
|
||||||
|
@ -170,6 +170,37 @@ docs:
|
||||||
static: true
|
static: true
|
||||||
params: "value: ...any"
|
params: "value: ...any"
|
||||||
returns: Promise<...any>
|
returns: Promise<...any>
|
||||||
|
- name: try
|
||||||
|
desc: |
|
||||||
|
Begins a Promise chain, calling a synchronous function and returning a Promise resolving with its return value. If the function errors, the returned Promise will be rejected with the error.
|
||||||
|
|
||||||
|
`Promise.try` is similar to [[Promise.promisify]], except the callback is executed instantly, and unlike `promisify`, yielding is not allowed with `try`.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
Promise.try(function()
|
||||||
|
return math.random(1, 2) == 1 and "ok" or error("Oh an error!")
|
||||||
|
end)
|
||||||
|
:andThen(function(text)
|
||||||
|
print(text)
|
||||||
|
end)
|
||||||
|
:catch(function(err)
|
||||||
|
warn("Something went wrong")
|
||||||
|
end)
|
||||||
|
```
|
||||||
|
static: true
|
||||||
|
params:
|
||||||
|
- name: callback
|
||||||
|
type:
|
||||||
|
kind: function
|
||||||
|
params: "...: ...any?"
|
||||||
|
returns: "...any?"
|
||||||
|
- name: "..."
|
||||||
|
type: "...any?"
|
||||||
|
desc: Arguments for the callback
|
||||||
|
returns:
|
||||||
|
- type: "Promise<...any?>"
|
||||||
|
desc: The return value of the passed callback.
|
||||||
|
|
||||||
- name: all
|
- name: all
|
||||||
desc: |
|
desc: |
|
||||||
Accepts an array of Promises and returns a new promise that:
|
Accepts an array of Promises and returns a new promise that:
|
||||||
|
|
|
@ -247,6 +247,13 @@ function Promise.reject(...)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
Begins a Promise chain, turning synchronous errors into rejections.
|
||||||
|
]]
|
||||||
|
function Promise.try(...)
|
||||||
|
return Promise.resolve():andThenCall(...)
|
||||||
|
end
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
Returns a new promise that:
|
Returns a new promise that:
|
||||||
* is resolved when all input promises resolve
|
* is resolved when all input promises resolve
|
||||||
|
|
|
@ -685,4 +685,17 @@ return function()
|
||||||
expect(finalValue).to.equal(1)
|
expect(finalValue).to.equal(1)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe("Promise.try", function()
|
||||||
|
it("should catch synchronous errors", function()
|
||||||
|
local errorText
|
||||||
|
Promise.try(function()
|
||||||
|
error('errortext')
|
||||||
|
end):catch(function(e)
|
||||||
|
errorText = e
|
||||||
|
end)
|
||||||
|
|
||||||
|
expect(errorText:find("errortext")).to.be.ok()
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end
|
end
|
Loading…
Reference in a new issue