You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ReadMe.md
+50-8
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ Jering.Javascript.NodeJS enables you to invoke javascript in [NodeJS](https://no
27
27
28
28
This library is built to be flexible; you can use a dependency injection (DI) based API or a static API, also, you can invoke both in-memory and on-disk javascript.
29
29
30
-
Here is an example of invoking javascript using the static API:
30
+
Static API example:
31
31
32
32
```csharp
33
33
stringjavascriptModule=@"
@@ -43,7 +43,7 @@ int result = await StaticNodeJSService.InvokeFromStringAsync<int>(javascriptModu
43
43
Assert.Equal(8, result);
44
44
```
45
45
46
-
And here is an example of invoking javascript using the DI based API:
46
+
DI based API example:
47
47
48
48
```csharp
49
49
stringjavascriptModule=@"
@@ -131,8 +131,10 @@ The following section on using `INodeJSService` applies to usage of `StaticNodeJ
131
131
132
132
### Using INodeJSService
133
133
#### Basics
134
-
To invoke javascript, we'll first need to create a NodeJS module that exports a function or an object containing functions. These functions must take
135
-
a callback as their first argument, and they must call the callback.
134
+
To invoke javascript, we'll first need to create a [NodeJS module](#nodejs-modules) that exports a function or an object containing functions. Exported functions can be of two forms:
135
+
136
+
##### Function With Callback Parameter
137
+
These functions must take a callback as their first argument, and they must call the callback.
136
138
The callback takes two optional arguments:
137
139
- The first argument must be an error or an error message. It must be an instance of type [`Error`](https://nodejs.org/api/errors.html#errors_class_error) or a `string`.
138
140
- The second argument is the result. It must be an instance of a JSON-serializable type, a `string`, or a [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable).
@@ -143,17 +145,18 @@ if you'd like to learn more about how asynchrony works in NodeJS).
143
145
144
146
This is a module that exports a valid function:
145
147
```javascript
146
-
module.exports= (callback) => {
147
-
...// Do something
148
+
module.exports= (callback, arg1, arg2, arg3) => {
149
+
...// Do something with args
148
150
149
151
callback(null, result);
150
152
}
151
153
```
154
+
152
155
And this is a module that exports an object containing valid functions:
153
156
```javascript
154
157
module.exports= {
155
-
doSomething: (callback) => {
156
-
...// Do something
158
+
doSomething: (callback, arg1) => {
159
+
...// Do something arg
157
160
158
161
callback(null, result);
159
162
},
@@ -165,6 +168,45 @@ module.exports = {
165
168
}
166
169
```
167
170
171
+
##### Async Function
172
+
Async functions are really just syntactic sugar for functions with callback parameters.
173
+
[Callbacks, Promises and Async/Await](https://medium.com/front-end-weekly/callbacks-promises-and-async-await-ad4756e01d90) provides a nice summary on how callbacks, promises and async/await work.
174
+
175
+
This is a module that exports a valid function:
176
+
```javascript
177
+
module.exports=async (arg1, arg2) => {
178
+
...// Do something with args
179
+
180
+
return result;
181
+
}
182
+
```
183
+
184
+
And this is a module that exports an object containing valid functions:
185
+
```javascript
186
+
module.exports= {
187
+
doSomething:async (arg1, arg2, arg3, arg4) => {
188
+
...// Do something with args
189
+
190
+
// async functions can explicitly return promises
191
+
returnnewPromise((resolve, reject) => {
192
+
resolve(result);
193
+
});
194
+
},
195
+
doSomethingElse:async (arg1) => {
196
+
...// Do something with arg
197
+
198
+
return result;
199
+
}
200
+
}
201
+
```
202
+
203
+
If an error is thrown it is caught and handled by the caller (error message is sent back to the calling .Net process):
204
+
```javascript
205
+
module.exports=async () => {
206
+
thrownewError('error message');
207
+
}
208
+
```
209
+
168
210
#### Invoking Javascript From a File
169
211
If we have a file named `exampleModule.js` (located in [`NodeJSProcessOptions.ProjectPath`](#nodejsprocessoptions)), with contents:
0 commit comments