How to interop with javascript code that changes an argument in place?

I want to interop with some javascript code. If I did things in javascript, then what I’d want to do would look like this:

var textToSVG
TextToSVG.load("https://myserver.com/myfont.ttf", (err, t2s) => {
  textToSVG = t2s; // This is the function I care about getting
});

What I want to get out of this code is the textToSVG function (t2s), but I don’t know how to get it a nice way in clojurescript because the javascript code is not in functional style. One way is like this:

(defonce text-to-svg (atom nil))
(TextToSVG/load
    "https://myserver.com/myfont.ttf"
    (fn [err t2s]
      (reset! text-to-svg t2s)))

Is that what you’d do, or is there a better way?

(edited for clarity)

if you want to guarantee that the fn will exist when you try to interact with it, you might put it in a promise that clients then chain off. or have it as a component in a clip system (clip can have async components) https://github.com/juxt/clip

Thanks. The whole async way of thinking is new to me. I might try out the promesa library so that I can write code as if it was synchronous, and halt the execution of everything else until the font has been loaded.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.