Created
December 6, 2021 23:30
-
-
Save tautologico/e366a2bc45890bc9a956e6a3c702ed5c to your computer and use it in GitHub Desktop.
Call a web API using promises and log the result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fetch an activity from the Bored API | |
type promise<+'a> = Js.Promise.t<'a> | |
@send external then: (promise<'a>, @uncurry ('a => promise<'b>)) => promise<'b> = "then" | |
@send external thenResolve: (promise<'a>, @uncurry ('a => 'b)) => promise<'b> = "then" | |
module Response = { | |
type t<'data> | |
@send external json: t<'data> => promise<'data> = "json" | |
} | |
type response = { | |
"activity": string, | |
"type": string, | |
"participants": int, | |
"price": float, | |
"link": string, | |
"key": string, | |
"accessibility": float | |
} | |
@val @scope("globalThis") external fetch: string => promise<Response.t<response>> = "fetch" | |
fetch("https://apis.scrimba.com/bored/api/activity") | |
->then(res => Response.json(res)) | |
->thenResolve(data => Js.log(data["activity"])) | |
->ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment