Skip to content

Instantly share code, notes, and snippets.

@tautologico
Created December 6, 2021 23:30
Show Gist options
  • Save tautologico/e366a2bc45890bc9a956e6a3c702ed5c to your computer and use it in GitHub Desktop.
Save tautologico/e366a2bc45890bc9a956e6a3c702ed5c to your computer and use it in GitHub Desktop.
Call a web API using promises and log the result
// 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