Skip to content

Commit 0aa55c4

Browse files
authored
Merge pull request #187727 from diberry/diberry/0207-azure-functions-context-done
[BULK] Azure Functions JS - context.done
2 parents 9b8462a + 60603b0 commit 0aa55c4

33 files changed

+174
-193
lines changed

articles/azure-functions/durable/durable-functions-bindings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Bindings for Durable Functions - Azure
33
description: How to use triggers and bindings for the Durable Functions extension for Azure Functions.
44
ms.topic: conceptual
5-
ms.date: 08/03/2021
5+
ms.date: 02/08/2022
66
ms.author: azfuncdf
77
---
88

@@ -85,7 +85,7 @@ module.exports = df.orchestrator(function*(context) {
8585
```
8686

8787
> [!NOTE]
88-
> The `durable-functions` library takes care of calling the `context.done` method when the generator function exits.
88+
> The `durable-functions` library takes care of calling the synchronous `context.done` method when the generator function exits.
8989
9090
# [Python](#tab/python)
9191

articles/azure-functions/durable/durable-functions-sequence.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Function chaining in Durable Functions - Azure
33
description: Learn how to run a Durable Functions sample that executes a sequence of functions.
44
author: cgillum
55
ms.topic: conceptual
6-
ms.date: 11/29/2019
6+
ms.date: 02/08/2022
77
ms.author: azfuncdf
88
ms.devlang: csharp, javascript, python
99
---
@@ -22,7 +22,7 @@ This article explains the following functions in the sample app:
2222
* `E1_SayHello`: An [activity function](durable-functions-bindings.md#activity-trigger) that prepends a string with "Hello".
2323
* `HttpStart`: An HTTP triggered [durable client](durable-functions-bindings.md#orchestration-client) function that starts an instance of the orchestrator.
2424

25-
### E1_HelloSequence orchestrator function
25+
## E1_HelloSequence orchestrator function
2626

2727
# [C#](#tab/csharp)
2828

@@ -58,7 +58,7 @@ All JavaScript orchestration functions must include the [`durable-functions` mod
5858

5959
1. The orchestrator function is a [generator function](/scripting/javascript/advanced/iterators-and-generators-javascript).
6060
2. The function is wrapped in a call to the `durable-functions` module's `orchestrator` method (here `df`).
61-
3. The function must be synchronous. Because the 'orchestrator' method handles calling 'context.done', the function should simply 'return'.
61+
3. The function must be synchronous. Because the 'orchestrator' method handles the final call to 'context.done', the function should simply 'return'.
6262

6363
The `context` object contains a `df` durable orchestration context object that lets you call other *activity* functions and pass input parameters using its `callActivity` method. The code calls `E1_SayHello` three times in sequence with different parameter values, using `yield` to indicate the execution should wait on the async activity function calls to be returned. The return value of each call is added to the `outputs` array, which is returned at the end of the function.
6464

@@ -94,7 +94,7 @@ The `context` object lets you call other *activity* functions and pass input par
9494

9595
---
9696

97-
### E1_SayHello activity function
97+
## E1_SayHello activity function
9898

9999
# [C#](#tab/csharp)
100100

@@ -148,7 +148,7 @@ Unlike the orchestrator function, an activity function needs no special setup. T
148148

149149
---
150150

151-
### HttpStart client function
151+
## HttpStart client function
152152

153153
You can start an instance of orchestrator function using a client function. You will use the `HttpStart` HTTP triggered function to start instances of `E1_HelloSequence`.
154154

articles/azure-functions/functions-bindings-cosmosdb-v2-input.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,10 +1090,9 @@ Here's the JavaScript code:
10901090

10911091
```javascript
10921092
// Change input document contents using Azure Cosmos DB input binding, using context.bindings.inputDocumentOut
1093-
module.exports = function (context) {
1094-
context.bindings.inputDocumentOut = context.bindings.inputDocumentIn;
1095-
context.bindings.inputDocumentOut.text = "This was updated!";
1096-
context.done();
1093+
module.exports = async function (context) {
1094+
context.bindings.inputDocumentOut = context.bindings.inputDocumentIn;
1095+
context.bindings.inputDocumentOut.text = "This was updated!";
10971096
};
10981097
```
10991098

@@ -1141,7 +1140,7 @@ Here's the *function.json* file:
11411140
Here's the JavaScript code:
11421141

11431142
```javascript
1144-
module.exports = function (context, req, toDoItem) {
1143+
module.exports = async function (context, req, toDoItem) {
11451144
context.log('JavaScript queue trigger function processed work item');
11461145
if (!toDoItem)
11471146
{
@@ -1151,8 +1150,6 @@ module.exports = function (context, req, toDoItem) {
11511150
{
11521151
context.log("Found ToDo item, Description=" + toDoItem.Description);
11531152
}
1154-
1155-
context.done();
11561153
};
11571154
```
11581155

@@ -1201,7 +1198,7 @@ Here's the *function.json* file:
12011198
Here's the JavaScript code:
12021199

12031200
```javascript
1204-
module.exports = function (context, req, toDoItem) {
1201+
module.exports = async function (context, req, toDoItem) {
12051202
context.log('JavaScript queue trigger function processed work item');
12061203
if (!toDoItem)
12071204
{
@@ -1211,8 +1208,6 @@ module.exports = function (context, req, toDoItem) {
12111208
{
12121209
context.log("Found ToDo item, Description=" + toDoItem.Description);
12131210
}
1214-
1215-
context.done();
12161211
};
12171212
```
12181213

@@ -1243,13 +1238,12 @@ The [configuration](#configuration) section explains these properties.
12431238
Here's the JavaScript code:
12441239

12451240
```javascript
1246-
module.exports = function (context, input) {
1241+
module.exports = async function (context, input) {
12471242
var documents = context.bindings.documents;
12481243
for (var i = 0; i < documents.length; i++) {
12491244
var document = documents[i];
12501245
// operate on each document
12511246
}
1252-
context.done();
12531247
};
12541248
```
12551249

articles/azure-functions/functions-bindings-cosmosdb-v2-output.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,23 +488,21 @@ The [configuration](#configuration) section explains these properties.
488488
Here's the JavaScript code:
489489

490490
```javascript
491-
module.exports = function (context) {
491+
module.exports = async function (context) {
492492

493493
context.bindings.employeeDocument = JSON.stringify({
494494
id: context.bindings.myQueueItem.name + "-" + context.bindings.myQueueItem.employeeId,
495495
name: context.bindings.myQueueItem.name,
496496
employeeId: context.bindings.myQueueItem.employeeId,
497497
address: context.bindings.myQueueItem.address
498498
});
499-
500-
context.done();
501499
};
502500
```
503501

504502
For bulk insert form the objects first and then run the stringify function. Here's the JavaScript code:
505503

506504
```javascript
507-
module.exports = function (context) {
505+
module.exports = async function (context) {
508506

509507
context.bindings.employeeDocument = JSON.stringify([
510508
{
@@ -519,8 +517,6 @@ For bulk insert form the objects first and then run the stringify function. Here
519517
"employeeId": "123457",
520518
"address": "A town far away"
521519
}]);
522-
523-
context.done();
524520
};
525521
```
526522

articles/azure-functions/functions-bindings-cosmosdb-v2-trigger.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,8 @@ Here's the binding data in the *function.json* file:
171171
Here's the JavaScript code:
172172

173173
```javascript
174-
module.exports = function (context, documents) {
174+
module.exports = async function (context, documents) {
175175
context.log('First document Id modified : ', documents[0].id);
176-
177-
context.done();
178176
}
179177
```
180178

articles/azure-functions/functions-bindings-cosmosdb.md

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,8 @@ Here's the binding data in the *function.json* file:
130130
Here's the JavaScript code:
131131

132132
```javascript
133-
module.exports = function (context, documents) {
133+
module.exports = async function (context, documents) {
134134
context.log('First document Id modified : ', documents[0].id);
135-
136-
context.done();
137135
}
138136
```
139137

@@ -926,10 +924,9 @@ Here's the JavaScript code:
926924

927925
```javascript
928926
// Change input document contents using Azure Cosmos DB input binding, using context.bindings.inputDocumentOut
929-
module.exports = function (context) {
930-
context.bindings.inputDocumentOut = context.bindings.inputDocumentIn;
931-
context.bindings.inputDocumentOut.text = "This was updated!";
932-
context.done();
927+
module.exports = async function (context) {
928+
context.bindings.inputDocumentOut = context.bindings.inputDocumentIn;
929+
context.bindings.inputDocumentOut.text = "This was updated!";
933930
};
934931
```
935932

@@ -976,7 +973,7 @@ Here's the *function.json* file:
976973
Here's the JavaScript code:
977974

978975
```javascript
979-
module.exports = function (context, req, toDoItem) {
976+
module.exports = async function (context, req, toDoItem) {
980977
context.log('JavaScript queue trigger function processed work item');
981978
if (!toDoItem)
982979
{
@@ -986,8 +983,6 @@ module.exports = function (context, req, toDoItem) {
986983
{
987984
context.log("Found ToDo item, Description=" + toDoItem.Description);
988985
}
989-
990-
context.done();
991986
};
992987
```
993988

@@ -1035,7 +1030,7 @@ Here's the *function.json* file:
10351030
Here's the JavaScript code:
10361031

10371032
```javascript
1038-
module.exports = function (context, req, toDoItem) {
1033+
module.exports = async function (context, req, toDoItem) {
10391034
context.log('JavaScript queue trigger function processed work item');
10401035
if (!toDoItem)
10411036
{
@@ -1045,8 +1040,6 @@ module.exports = function (context, req, toDoItem) {
10451040
{
10461041
context.log("Found ToDo item, Description=" + toDoItem.Description);
10471042
}
1048-
1049-
context.done();
10501043
};
10511044
```
10521045

@@ -1077,13 +1070,12 @@ The [configuration](#input---configuration) section explains these properties.
10771070
Here's the JavaScript code:
10781071

10791072
```javascript
1080-
module.exports = function (context, input) {
1073+
module.exports = async function (context, input) {
10811074
var documents = context.bindings.documents;
10821075
for (var i = 0; i < documents.length; i++) {
10831076
var document = documents[i];
10841077
// operate on each document
10851078
}
1086-
context.done();
10871079
};
10881080
```
10891081

@@ -1403,16 +1395,14 @@ The [configuration](#output---configuration) section explains these properties.
14031395
Here's the JavaScript code:
14041396

14051397
```javascript
1406-
module.exports = function (context) {
1398+
module.exports = async function (context) {
14071399

14081400
context.bindings.employeeDocument = JSON.stringify({
14091401
id: context.bindings.myQueueItem.name + "-" + context.bindings.myQueueItem.employeeId,
14101402
name: context.bindings.myQueueItem.name,
14111403
employeeId: context.bindings.myQueueItem.employeeId,
14121404
address: context.bindings.myQueueItem.address
14131405
});
1414-
1415-
context.done();
14161406
};
14171407
```
14181408

articles/azure-functions/functions-bindings-event-grid-output.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,13 @@ module.exports = async function (context, myTimer) {
282282
data: "event-data",
283283
eventTime: timeStamp
284284
};
285-
context.done();
286285
};
287286
```
288287

289288
Here's JavaScript code that creates multiple events:
290289

291290
```javascript
292-
module.exports = function(context) {
291+
module.exports = async function(context) {
293292
var timeStamp = new Date().toISOString();
294293

295294
context.bindings.outputEvent = [];
@@ -310,7 +309,6 @@ module.exports = function(context) {
310309
data: "event-data",
311310
eventTime: timeStamp
312311
});
313-
context.done();
314312
};
315313
```
316314

articles/azure-functions/functions-bindings-event-grid-trigger.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,11 @@ Here's the binding data in the *function.json* file:
264264
Here's the JavaScript code:
265265

266266
```javascript
267-
module.exports = function (context, eventGridEvent) {
267+
module.exports = async function (context, eventGridEvent) {
268268
context.log("JavaScript Event Grid function processed a request.");
269269
context.log("Subject: " + eventGridEvent.subject);
270270
context.log("Time: " + eventGridEvent.eventTime);
271271
context.log("Data: " + JSON.stringify(eventGridEvent.data));
272-
context.done();
273272
};
274273
```
275274

articles/azure-functions/functions-bindings-example.md

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: craigshoemaker
66
ms.topic: reference
77
ms.devlang: csharp, javascript
88
ms.custom: devx-track-csharp
9-
ms.date: 02/18/2019
9+
ms.date: 02/08/2022
1010
ms.author: cshoe
1111
---
1212

@@ -48,7 +48,7 @@ To view and edit the contents of *function.json* in the Azure portal, click the
4848
> [!NOTE]
4949
> The value of `connection` is the name of an app setting that contains the connection string, not the connection string itself. Bindings use connection strings stored in app settings to enforce the best practice that *function.json* does not contain service secrets.
5050
51-
## C# script example
51+
# [C# script](#tab/csharp)
5252

5353
Here's C# script code that works with this trigger and binding. Notice that the name of the parameter that provides the queue message content is `order`; this name is required because the `name` property value in *function.json* is `order`
5454

@@ -78,27 +78,7 @@ public class Person
7878
}
7979
```
8080

81-
## JavaScript example
82-
83-
The same *function.json* file can be used with a JavaScript function:
84-
85-
```javascript
86-
// From an incoming queue message that is a JSON object, add fields and write to Table Storage
87-
// The second parameter to context.done is used as the value for the new row
88-
module.exports = function (context, order) {
89-
order.PartitionKey = "Orders";
90-
order.RowKey = generateRandomId();
91-
92-
context.done(null, order);
93-
};
94-
95-
function generateRandomId() {
96-
return Math.random().toString(36).substring(2, 15) +
97-
Math.random().toString(36).substring(2, 15);
98-
}
99-
```
100-
101-
## Class library example
81+
# [C# class library](#tab/csharp-class-library)
10282

10383
In a class library, the same trigger and binding information &mdash; queue and table names, storage accounts, function parameters for input and output &mdash; is provided by attributes instead of a function.json file. Here's an example:
10484

@@ -128,6 +108,29 @@ public class Person
128108
}
129109
```
130110

111+
# [JavaScript](#tab/javascript)
112+
113+
114+
115+
The same *function.json* file can be used with a JavaScript function:
116+
117+
```javascript
118+
// From an incoming queue message that is a JSON object, add fields and write to Table Storage
119+
module.exports = async function (context, order) {
120+
order.PartitionKey = "Orders";
121+
order.RowKey = generateRandomId();
122+
123+
context.bindings.order = order;
124+
};
125+
126+
function generateRandomId() {
127+
return Math.random().toString(36).substring(2, 15) +
128+
Math.random().toString(36).substring(2, 15);
129+
}
130+
```
131+
132+
---
133+
131134
You now have a working function that is triggered by an Azure Queue and outputs data to Azure Table storage.
132135

133136
## Next steps

0 commit comments

Comments
 (0)