mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 23:50:03 +00:00
Update README.md
This commit is contained in:
parent
c285a2a16a
commit
5514aefe64
1 changed files with 8 additions and 7 deletions
15
README.md
15
README.md
|
@ -30,22 +30,23 @@ local HttpService = game:GetService("HttpService")
|
||||||
-- Ideally, you do this once per project per async method that you use.
|
-- Ideally, you do this once per project per async method that you use.
|
||||||
local function httpGet(url)
|
local function httpGet(url)
|
||||||
return Promise.new(function(resolve, reject)
|
return Promise.new(function(resolve, reject)
|
||||||
local ok, result = pcall(HttpService.GetAsync, HttpService, url)
|
local result = HttpService:JSONDecode(HttpService:GetAsync(url))
|
||||||
|
|
||||||
if ok then
|
if result.ok then
|
||||||
resolve(result)
|
resolve(result.data)
|
||||||
else
|
else
|
||||||
reject(result)
|
reject(result.error)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Usage
|
-- Usage
|
||||||
httpGet("https://google.com")
|
httpGet("https://some-api.example")
|
||||||
:andThen(function(body)
|
:andThen(function(body)
|
||||||
print("Here's the Google homepage:", body)
|
print("Here's the web api result:", body)
|
||||||
end)
|
end)
|
||||||
:catch(function(err)
|
:catch(function(err)
|
||||||
warn("We failed to get the Google homepage!", err)
|
warn("Web api encountered an error:", err)
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue