-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabort-execution.js
48 lines (43 loc) · 1.15 KB
/
abort-execution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { StateMachine, ExecutionAbortedError } from 'aws-local-stepfunctions';
const machineDefinition = {
StartAt: 'AddNumbers',
States: {
AddNumbers: {
Type: 'Task',
Resource: 'arn:aws:lambda:us-east-1:123456789012:function:AddNumbers',
Next: 'WaitBeforeEndingExecution',
},
WaitBeforeEndingExecution: {
Type: 'Wait',
Seconds: 5,
End: true,
},
},
};
const stateMachine = new StateMachine(machineDefinition);
const myInput = { num1: 5, num2: 7 };
// Execute the state machine
const execution = stateMachine.run(myInput, {
overrides: {
taskResourceLocalHandlers: {
AddNumbers: (input) => {
return input.num1 + input.num2;
},
},
},
});
// Abort the execution after 2 seconds
setTimeout(() => {
execution.abort();
}, 2000);
try {
const result = await execution.result;
console.log(result); // If not aborted, would log 12 once execution finishes
} catch (e) {
if (e instanceof ExecutionAbortedError) {
// Since execution was aborted, type of error is `ExecutionAbortedError`
console.log('Execution was aborted');
} else {
console.error('Some other error', e);
}
}