Nested Callbacks

This is a type of pattern that can be used for asynchronous programming
Asynchronous programming with callback functions frequently requires you to nest the returned result of one callback within two or more callbacks.
When you need to perform two or more asynchronous operations, you can nest one "Async" call inside another.
You can use either anonymous functions or named functions to achieve this.


Using Anonymous functions

Office.context.document.bindings.getByIdAsync('myBinding', function (bindingResult) {  
    bindingResult.value.getDataAsync(function (getResult) {
        if (getResult.status == Office.AsyncResultStatus.Failed) {
            write('Action failed. Error: ' + asyncResult.error.message);
        } else {
            write('Data has been read successfully.');
        }
    });
});

// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message;
}

Using Named functions

Office.context.document.bindings.getByIdAsync('myBinding', deleteAllData);


function deleteAllData(asyncResult) { 
    asyncResult.value.deleteAllDataValuesAsync(showResult);
}

function showResult(asyncResult) {
    if (asyncResult.status == Office.AsyncResultStatus.Failed) {
        write('Action failed. Error: ' + asyncResult.error.message);
    } else {
        write('Data has been deleted successfully.');
    }
}

// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message;
}

Async



CallBack

The callback function you pass as the callback argument to an "Async" method must declare a single parameter that the add-in


runtime will use to provide access to an AsyncResult object when the callback function executes. You can write:
An anonymous function that must be written and passed directly in line with the call to the "Async" method as the callback


parameter of the "Async" method.
A named function, passing the name of that function as the callback parameter of an "Async" method


* move the Named and Anonymous to this page


The function 'fn_getDataFromSelection' is a function that is called from a button click



© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext