biwascheme icon indicating copy to clipboard operation
biwascheme copied to clipboard

Javascript Fetch API

Open 80aff123-9163-4f3e-a93d-4a2f35af9be1 opened this issue 4 years ago • 3 comments

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?

yhara avatar Nov 20 '20 13:11 yhara

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.

jcubic avatar Nov 20 '20 14:11 jcubic

I need that too. Keep in the loop.

amirouche avatar Aug 08 '21 10:08 amirouche