locked
Is it memory intense to keep promises around for later access of the result?

    Question

  • I am using this pattern that I remember promises to access their result later. 

    var userNamePromise;
    function getUserNameAsync() {
      return userNamePromise || userNamePromise = WinJS.Promise.join(
        domainName: UserInformation.getDomainNameAsync()
        displayName: UserInformation.getDisplayNameAsync()
      ).then(function (result) {
        return result.domainUser ? result.domainUser : result.displayName;
      })
    }
    

    So no matter how often getUserNameAsync is called, it will only run all those async operations once.

    Another way would be to set a userNameResult variable in the completion handler and just return WinJS.Promise.as(userNameResult) when it has been filled already in the completion handler.

    I am not sure how much memory such promises take up.

    Wednesday, June 11, 2014 11:33 PM

Answers

  • There's always a tradeoff between caching and memory utilization. Keeping information in memory is good for speed but you should test the application to see exactly how much memory it's expected to take over a period of time. It may not take up too much at all.

    That said, I prefer the use of the userNameResult variable.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, June 12, 2014 1:34 PM
    Moderator