biwascheme
biwascheme copied to clipboard
Javascript Fetch API
Is there interest in implementing the fetch api?
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
I'd like to help.
It's definitely nice to have. First we need to design Scheme API. Do you have an idea?
If you want inspiration you can look at my Scheme interpreter where I don't need to add anything special to the library to have support for fetch API.
(--> (fetch "https://lips.js.org") (text) (match /<title>([^>]+)<\/title>/) 1)
but my implementation is not pure scheme like BiwaScheme, it have literal regexes and automatic async/await (I plant to add quoting of promises in the future).
For BiwaScheme you would need interface because, unfortunately it don't expose API to get global JavaScript objects so you need to write everything in JavaScript or use js-eval, here is my attempt of using the API without any extra JS code:
(let ((promise (js-call (js-eval "fetch") "https://lips.js.org")))
(set! promise (js-invoke promise "then" (lambda (x)
(js-invoke x "text"))))
(js-invoke promise "then" (lambda (text)
(let ((m (js-invoke text "match" (new RegExp "<title>([^>]+)</title>"))))
(if (null? m)
(alert "NOPE")
(alert (js-ref m 1)))))))
but it don't work and don't throw exception, not sure why. Any clue why this code don't work? Not sure if you prefer write JavaScript to create the API for fetch but this can just be a macro.
I need that too. Keep in the loop.