Skip to content

Callback arguments are not always optional #74

Open
@irahopkinson

Description

@irahopkinson

It seems the callback arguments are only optional when the return type is string. For anything else they are not always optional. For example, the following all work as expected:

string resultStr = await StaticNodeJSService
    .InvokeFromStringAsync<string>("module.exports = (callback, message) => callback(null, message);", args: new[] { "success" });
resultStr = await StaticNodeJSService
    .InvokeFromStringAsync<string>("module.exports = (callback, message) => callback(undefined, message);", args: new[] { "success" });
resultStr = await StaticNodeJSService
    .InvokeFromStringAsync<string>("module.exports = (callback) => callback(undefined, 'success');");
resultStr = await StaticNodeJSService
    .InvokeFromStringAsync<string>("module.exports = (callback) => callback();");
int resultNum = await StaticNodeJSService
    .InvokeFromStringAsync<int>("module.exports = (callback) => callback(undefined, 7);");
object resultObj = await StaticNodeJSService
    .InvokeFromStringAsync<object>("module.exports = (callback) => callback(undefined, { result: 'success' });");
resultObj = await StaticNodeJSService
    .InvokeFromStringAsync<object>("module.exports = (callback) => callback(undefined, {});");

And the following throw the expected exception:

resultStr = await StaticNodeJSService
    .InvokeFromStringAsync<string>("module.exports = (callback) => callback(new Error('Expected exception.'));");
resultNum = await StaticNodeJSService
    .InvokeFromStringAsync<int>("module.exports = (callback) => callback(new Error('Expected exception.'));");
resultObj = await StaticNodeJSService
    .InvokeFromStringAsync<object>("module.exports = (callback) => callback(new Error('Expected exception.'));");

However, the following I would expect to work:

resultNum = await StaticNodeJSService
    .InvokeFromStringAsync<int>("module.exports = (callback) => callback();");
resultNum = await StaticNodeJSService
    .InvokeFromStringAsync<int>("module.exports = (callback) => callback(undefined, NaN);");
resultObj = await StaticNodeJSService
    .InvokeFromStringAsync<object>("module.exports = (callback) => callback();");

But they throw this exception (each have different inner exceptions):

Exception has occurred: CLR/System.Text.Json.JsonException
Exception thrown: 'System.Text.Json.JsonException' in System.Private.CoreLib.dll: 'The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.'
 Inner exceptions found, see $exception in variables window for more details.

I would preferred a fix so that they work but at the least, the documentation could be changed to reflect reality, particularly section https://github.com/JeringTech/Javascript.NodeJS#function-with-callback-parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions