Skip to content

Instantly share code, notes, and snippets.

@winterdl
Forked from sjordine/asyncJS.swift
Created January 19, 2021 07:29
Show Gist options
  • Select an option

  • Save winterdl/0654c6e83eb60c695641f7259da86a7d to your computer and use it in GitHub Desktop.

Select an option

Save winterdl/0654c6e83eb60c695641f7259da86a7d to your computer and use it in GitHub Desktop.
if let context = JSContext() {
let asynchronousCall: @convention(block) (JSValue) -> Void = { (callback) in
loadCatFact { (catFact) in
callback.call(withArguments: [catFact])
}
}
let printText:@convention(block) (String) -> Void = { (text) in
print("\(text)")
}
context.setObject(asynchronousCall,
forKeyedSubscript: "loadFact" as NSString)
context.setObject(printText,
forKeyedSubscript: "print" as NSString)
let result = context.evaluateScript("""
print("before");
loadFact(result => {
print("Raw data: " + result);
fact = JSON.parse(result);
print("Fact: " + fact.fact);
});
print("after");
""")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment