-
Suggestion
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
Qt Script lets us do things like this; it would be helpful to have a similar ability in QJSEngine:
QScriptValue ScriptContext::scriptAbortAfter(QScriptContext* ctx, QScriptEngine& eng)
{
try
{
if (ctx->argumentCount() < 2)
throw Exception(L"abortAfter(timeout, callback[, callback parameters...]) takes at least 2 parameters.");
if (!ctx->argument(0).isNumber())
throw Exception(L"Parameter 1 (timeout) must be an integer specifying the timeout in milliseconds.");
if (!ctx->argument(1).isFunction())
throw Exception(L"Parameter 2 (callback) must be a function.");
int timeout = ctx->argument(0).toInteger();
QScriptValue function = ctx->argument(1);
QScriptValueList callbackParameters;
for (int i = 2; i < ctx->argumentCount(); ++i)
callbackParameters.push_back(ctx->argument(i));
auto future = std::async(std::launch::async, [&]() {
QScriptValue value;
value = function.call(ctx->thisObject(), callbackParameters);
return value;
});
}
...
}
Side note
For this particular example, an alternative solution might be to add support for ECMAScript 2017. Then, this example C++ code can be replaced by native JS async functions