Need to execute a functionality twice on a single event.

guess_me_if_u_can 126 Reputation points
2020-02-13T15:15:53.693+00:00

In my scenario, my uwp app need to supports both online and offline mode. When an Event triggers and fetching data from the server, i need to serve the offline data in the mean time.

function getData()
{
    // calls the server for online data
    // return offline data
    // return online data 
}

function main()
{
    var data = getData();
    processData(data);
    // ...
}

In this case, when the main executes, getData() need to initiates the API call and return the offline data first and the main need to proceeds by processData(offlineData). Once getData() obtains the Online Data, it need to send the value back to main again and proceed by proceeds by processData(onlineData). Kindly assist me the way to acheive this requirement.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points
    2020-02-14T02:03:34.817+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I can provide an idea to solve your problem:

    1. Create getDataFromWeb() and getDataFromLocal() methods instead of the general getData() method.
    2. Create a global variable, assuming the variable name is totalData.
    3. First call getDataFromLocal() to assign the obtained value to totalData, and then continue to call getDataFromWeb(). If the value is obtained, assign the new value to totalData.
    4. Display the content of totalData on the UI by binding.

    Here are some documents that might be helpful:

    ---

    From the code you provided, the programming language you may use is Javascript. If so, you need to know Javascript about how to create Ajax requests.

    Thanks.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.