Have Callback Work Synchronously In Javascript
I am working with a package called DSBridge to connect a mobile application (iOS code in this case) to javascript code containing the main logic of my application. This involves ru
Solution 1:
You can pass a callback to the read()
function and invoke it when the underlying Objective-C code returns
functionread(clb) {
data = "reading";
dsBridge.call("read", data, function (read_result) {
clb(read_result);
});
}
and your caller
read(res => {
//result available hereconsole.log(res);
})
Alternatively with Promises
functionread() {
data = "reading";
returnnewPromise(resolve => {
dsBridge.call("read", data, function (read_result) {
resolve(read_result);
});
});
}
and the caller
read()
.then(res => {
//result available hereconsole.log(res);
})
Post a Comment for "Have Callback Work Synchronously In Javascript"