React Native Requires Chilkat v11.4.0+
React Native
Call a JavaScript Function Returning a Boolean
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns a boolean.Chilkat React Native Downloads
import { Js, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This is the JavaScript function we'll call:
// function isEven(number) {
// return number % 2 === 0;
// }
const sbScript = new StringBuilder();
sbScript.append('function isEven(number) { return number % 2 === 0; }');
const js = new Js();
const result = new JsonObject();
result.emitCompact = false;
// Call Eval to add the function to the context's global object
try {
await js.evalAsync(sbScript, result);
} catch {
// Examine the result for an exception.
console.log(result.emit());
// Also examine the LastErrorText.
console.log(js.lastErrorText);
return;
}
// ------------------------------------------------------------------------------
// Call the function isEven(8)
const funcCall = new JsonObject();
// Create JSON specifying the function name and arguments
// {
// "name": "isEven",
// "args": [ 8 ]
// }
funcCall.updateString('name', 'isEven');
funcCall.updateInt('args[0]', 8);
try {
await js.callFunctionAsync(funcCall, result);
} catch {
// Examine the result for an exception.
console.log(result.emit());
// Also examine the LastErrorText.
console.log(js.lastErrorText);
return;
}
console.log(result.emit());
// Output:
// {
// "type": "bool",
// "value": true
// }
const retval = result.boolOf('value');
console.log(retval);
// Output:
// true
}