By 06/07/2015 14 Comments

TypeScript Callback Function

Define Callback Function:

  • A callback function in TypeScript is a function which is passed from another function as its parameter.
  • A callback function is called automatically in another function when another/normal function is invoked/called explicitly.
  • A callback function is passed from a normal function as an argument in order to invoke it.

Example:

/* Following is a void type function which has an argument array as its parameter. This function is a callback function. */
function list(...products: string[]) {
    var pList: string[] = products;
    document.write("Product list: <br/>");

// loop through each element and print them
    for (var index = 0; index < pList.length; index++) {
        document.write( pList[index]);

    }
}

/* Following is a function which has 2 parameters, 1st parameter is a callback function (callback) with its argument (...products: string[]) and its return type (void). 2nd parameter is an argument string type array.*/
function getList(callback: (...products: string[]) => void, ...pList: string[]) {

/* Following, a callback function is invoked/called and pass an array from it as an argument. */
        callback("" + pList);

}

/* Call a getList() function, this function passes 2 arguments, 1st is a callback function which is list() and 2nd arguments are multiple string values which is received by an argument array. */ 
getList(list, "Laptop", "Mobile phone", "Iron", "Television", "Table");

 







Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .