Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8384539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:25:43+00:00 2026-06-09T17:25:43+00:00

I currently have an issue with a file read in a Windows 8/WinRT application.

  • 0

I currently have an issue with a file read in a Windows 8/WinRT application. I have a simple navigation style app, several pages have access to the same data and I have a data.js file that defines a namespace (Data) with a number of members. One part of the application saves items to a txt file stored in the applications local data folder. But on some of the other pages I need to read this in or check for the existence of an item within the list of previously saved items. To do this I added another method into the data.js file. The trouble is, when I call this method to check for the existence of an item, it doesn’t return the value straight away due to the async nature, but the rest of code in the page specific js file still seems to execute before it jumps back into the parsing. This means that the logic to check for an item doesn’t seem to work. I have a feeling it’s down to my use of either .done or .then but my code is as follows:

  DATA.JS
   var doesItemExist= function(item_id){

    var appFolder = Windows.Storage.ApplicationData.current.localFolder;
   //note I've tried this with and without the first "return" statement
   return  appFolder.getFileAsync(dataFile).then(function (file) {
        Windows.Storage.FileIO.readTextAsync(file).done(function (text) {
            try {
                var json = JSON.parse(text);
                if (json) {
                    for (var i = 0; i < json.items.length; i++) {
                        var temp_item = json.items[i];
                        if (temp_item.id === item_id) {
                            return true;
                            break;
                        }
                    }
                } else {
                    return false;
                }
            } catch (e) {
                return false;
                console.log(e);
            }
        }, function (e) { return false;console.log(e); });
    }, function (e) { // error handling
        return false;
        console.log(e);

    });
}
 WinJS.Namespace.define("Data", {
    doesItemExist: doesItemExist
}); //all of the above is wrapped in a self executing function

Then on Page.js I have the following:

    var add = document.getElementById('add');
        if (Data.doesItemExist(selected_item.id)) {
            add.style.display = 'block';
        } else {
            add.style.display = 'none';
        }

All the variables here are assigned and debugging doesn’t produce any errors, control just appears to go back to the if/else statement after it hits the getFileAsync but before it even goes through the for loop. But subsequently it does go in to the for loop but after the if statement has finished. I’m guessing this is down to the async nature of it all, but I’m not sure how to get around it. Any ideas?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:25:45+00:00Added an answer on June 9, 2026 at 5:25 pm

    A Promise should work here.

    I created a new Navigation app, and added a Data.js file containing the following code:

    (function () {
        var appData = Windows.Storage.ApplicationData;
    
        function doesItemExist(item_id) {
            return new WinJS.Promise(
                function (completed, error, progress) {
                    var exists = false;
                    appData.current.localFolder.createFileAsync("data.txt", Windows.Storage.CreationCollisionOption.openIfExists).then(
                        function (file) {
                            Windows.Storage.FileIO.readTextAsync(file).then(
                                function (fileContents) {
                                    if (fileContents) {
                                        if (fileContents = "foo!") {
                                            completed(true);
                                        }
                                        else {
                                            completed(false);
                                        }
                                    }
                                    else {
                                        completed(false);
                                    }
                                }
                            );
                        },
                        function (e) {
                            error(e);
                        }
                    );
                }
            );
        }
    
        WinJS.Namespace.define("Data", {
            doesItemExist: doesItemExist
        });
    })();
    

    Note that I’ve simplified the code for retrieving and parsing the file, since that’s not really relevant to the problem. The important part is that once you’ve determined whether the item exists, you call completed(exists) which triggers the .then or .done of the Promise you’re returning. Note that you’d call error(e) if an exception occurs, as I’m doing if there’s an exception from the call to createFileAsync (I use this call rather than getFileAsync when I want to be able to either create a file if it does not exist, or return the existing file if it does, using the openIfExists option).

    Then, in Home.js, I added the following code to the ready handler:

    var itemExists;
    
    var itemExistsPromise = Data.doesItemExist(42);
    itemExistsPromise = itemExistsPromise.then(function (exists) {
        itemExists = exists;
        var content = document.getElementById("content");
        content.innerText = "ItemExists is " + itemExists;
    });
    
    itemExistsPromise.done(function () {
        var a = 42;
    });
    
    var b = 0;
    

    The code above sets the variable itemExistsPromise to the returned promise from the function in Data.js, and then uses an anonymous function in the .then function of the Promise to set the variable itemExists to the Boolean value returned from the doesItemExist Promise, and grabs the <p> tag from Home.html (I added an id so I could get to it from code) and sets its text to indicate whether the item exists or not). Because I’m calling .then rather than .done, the call returns another promise, which is passed into the itemExistsPromise variable.

    Next, I call itemExistsPromise.done to do any work that has to wait until after the work performed in the .then above it.

    If you set a breakpoint on the lines “var a = 42” and “var b = 0” (only included for the purpose of setting breakpoints) as well as on the line “itemExists = exists”, you should find that this gives you the control you need over when the various parts are executed.

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange issue . I am currently working on a mail app
I currently have a issue with firefox, where all other browser behave in the
I'm having an issue with multiple markers on google maps - I currently have
I have a modeling issue with Symfony2/Doctrine2. I am currently trying to pass a
So i have a lack of knowledge issue with this. I'm currently streaming my
I currently store a serialized XML file in the application directory that contains all
I have a C# app that creates a settings file for itself to store
Background In a targeted issue tracking application (in django) users are able add file
I am trying to figure out when and how does Windows update File Access
I have an application that logs information to a daily text file every second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.