Question

I'm trying to model my Application with UML Activity Diagrams. I'm using JavaScript and Node.js and lots of asynchronous callbacks. Here is what I came up with:

Activity Diagram

What do you think? Do you understand whats going on? I'm using a "generic connector" to associate the callback with the action ("run MyClass.myMethod) and a fork-node to show the "parallel" execution. I did not find a written word about callbacks in Activity Diagrams anywhere on the web or my books.

EDIT This would be the JavaScript code for the diagram:

var MyClass = function () {
    //constructor
};
MyClass.prototype = {
    myMethod : function(cb) {
        //this is an async method
        var result = 5 + 5;
        setTimeout(function () {
            cb(null, result);
        },100); //execute Callback after 100ms
    }
};

//instanciate a MyClass Object
var myClassInstance = new MyClass();

//create a callback function that prints the result
var callbackFunction = function (err,result) {
    console.log(result);
};

myClassInstance.myMethod(callbackFunction);
console.log('I am first');
Was it helpful?

Solution

A correct way to show callbacks is the following:⁺

You instanciate a Object, pass it to the target-port and call the specified operation (calculateTime) on it. The control flow continues immediately and when the asynchronous operation finishes, the result is transfered to the result-pin. Arguments for the async call can be specified by adding more object-(in)put pins to the Action.

async callback Activity Diagram

⁺(see UML Spec. 11-08-06 11.3.10 CallOperationAction/ 11.3.8 CallAction)

OTHER TIPS

The best way to show callbacks in activity diagrams would be signals. Just take into account that signals are completely asynchronous (full detachment between sender and receiver) and I am not sure this is the case in javascript. But they have a closer meaning to what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top