locked
Promises, Anonymous, Literal Functions

    Question

  • The Anonymous functions and Literal Functions kind of throw me off.  I am wondering if there is a good tutorial that explains Promises, Anonymous, and Literal functions a little better.  I feel I am getting the condensed here is everything in one short no foundation explanation tutorials. I do have a C# background but never used anything like the three I am having troubles with in Javascript in a real world scenario.
    Friday, January 4, 2013 6:37 PM

Answers

  • A function literal or anonymous function is when you declare a function this way:

    <script type="text/javascript">
    var variablename = function(Argument List){
                           Function Body 
                       };
    </script>

    You can then call variablename(); from any place within the same scope as the declaration of the variable.

    Function literals or anonymous functions can be parameters of Promises.

    There is a great free EBook by Microsoft Press Programming Windows 8 Apps with HTML, CSS and Javascript that in its chapter 3, has a section called Completing the Promises Story that explains very well what Promises are and what do they do. Highly recommended reading (the whole book) and it's free.

    Basically, lets say you have an async operation (reading of a file, HTTP GET, etc) and once that is done, you want to do something with that information, a Promise is a construct that let's you fire that async operation with the promise (hence the name) that once it completes, it will execute some other function or code. Aditionally you can chain Promises using the .then() operator and finalize a chain of Promises with a .done() operator (its very well explained in the book).

    For example, let's say I have a function that does some heavy work and reads files and you want to do another async operation once that first one is finished, without Promises it would be kind of messy, but with Promises its as simple as:

    new WinJS.Promise(function(){
    //do some file reading or other work
    return data;
    }).done(
    function(result){
    //Handler on Success, receives previous Promise return value
    //do some other async work
    },
    function(error){
    //Handler on Error
    console.log(error);//for example
    };


    An error handler is optional but I always use it to handle any error logic.

    Any Async operator or class in WinJS implements Promises without you needing to declare it specifically (using a "new Promise"), for example, WinJS.xhr

    WinJS.xhr("http://somesource.com/rss").done(function(result){
    //do something with the rss data
    ;
    })

    The File reading classes implement Promises too.

    I hope this info helps you going on the right track and good luck!

    PD: Quickstart: using promises , Error handling on Promises and Chaining Promises


    • Marked as answer by Song Tian Thursday, January 10, 2013 9:33 AM
    Friday, January 4, 2013 7:15 PM
  • Hi LluvianBlack,

    For using "Promise" in windows store javascript app, you can take a look at the following MSDN reference:

    #Asynchronous programming in JavaScript (Windows Store apps) (Windows)
    http://msdn.microsoft.com/en-us/library/windows/apps/hh700330.aspx

    and the following one is very good for you to learn how to deal with multiple asynchronous calls sequentially and avoid using complicated nested callback anonymous functions:


    #Chaining promises (Windows Store apps using JavaScript and HTML) (Windows)
    http://msdn.microsoft.com/en-us/library/windows/apps/Hh700334.aspx

    In addition, there is a sdk sample for demonstrating WinJS.Promise usage which you can also take a look:

    #WinJS Promise sample
    http://code.msdn.microsoft.com/windowsapps/Promise-e1571015

    The book Ealsur mentioned is really a good one for you to get familar with windows store app develoment via HTML5+ js.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Song Tian Thursday, January 10, 2013 9:33 AM
    Monday, January 7, 2013 8:19 AM
    Moderator

All replies

  • A function literal or anonymous function is when you declare a function this way:

    <script type="text/javascript">
    var variablename = function(Argument List){
                           Function Body 
                       };
    </script>

    You can then call variablename(); from any place within the same scope as the declaration of the variable.

    Function literals or anonymous functions can be parameters of Promises.

    There is a great free EBook by Microsoft Press Programming Windows 8 Apps with HTML, CSS and Javascript that in its chapter 3, has a section called Completing the Promises Story that explains very well what Promises are and what do they do. Highly recommended reading (the whole book) and it's free.

    Basically, lets say you have an async operation (reading of a file, HTTP GET, etc) and once that is done, you want to do something with that information, a Promise is a construct that let's you fire that async operation with the promise (hence the name) that once it completes, it will execute some other function or code. Aditionally you can chain Promises using the .then() operator and finalize a chain of Promises with a .done() operator (its very well explained in the book).

    For example, let's say I have a function that does some heavy work and reads files and you want to do another async operation once that first one is finished, without Promises it would be kind of messy, but with Promises its as simple as:

    new WinJS.Promise(function(){
    //do some file reading or other work
    return data;
    }).done(
    function(result){
    //Handler on Success, receives previous Promise return value
    //do some other async work
    },
    function(error){
    //Handler on Error
    console.log(error);//for example
    };


    An error handler is optional but I always use it to handle any error logic.

    Any Async operator or class in WinJS implements Promises without you needing to declare it specifically (using a "new Promise"), for example, WinJS.xhr

    WinJS.xhr("http://somesource.com/rss").done(function(result){
    //do something with the rss data
    ;
    })

    The File reading classes implement Promises too.

    I hope this info helps you going on the right track and good luck!

    PD: Quickstart: using promises , Error handling on Promises and Chaining Promises


    • Marked as answer by Song Tian Thursday, January 10, 2013 9:33 AM
    Friday, January 4, 2013 7:15 PM
  • Hi LluvianBlack,

    For using "Promise" in windows store javascript app, you can take a look at the following MSDN reference:

    #Asynchronous programming in JavaScript (Windows Store apps) (Windows)
    http://msdn.microsoft.com/en-us/library/windows/apps/hh700330.aspx

    and the following one is very good for you to learn how to deal with multiple asynchronous calls sequentially and avoid using complicated nested callback anonymous functions:


    #Chaining promises (Windows Store apps using JavaScript and HTML) (Windows)
    http://msdn.microsoft.com/en-us/library/windows/apps/Hh700334.aspx

    In addition, there is a sdk sample for demonstrating WinJS.Promise usage which you can also take a look:

    #WinJS Promise sample
    http://code.msdn.microsoft.com/windowsapps/Promise-e1571015

    The book Ealsur mentioned is really a good one for you to get familar with windows store app develoment via HTML5+ js.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Song Tian Thursday, January 10, 2013 9:33 AM
    Monday, January 7, 2013 8:19 AM
    Moderator