Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Question About JS Async

  • 10-07-2018 7:31pm
    #1
    Registered Users, Registered Users 2 Posts: 2,815 ✭✭✭


    I have a question about JS async calls please.

    So, whenever I call an API, I wrap the call in a promise and anything I want to do with the returned data is performed once I get a response, i.e.
           SomeService.getSomething(id).then(function (data) {
                    //do something with data
            });
    

    Take the following code:
    SomeService.FunctionCall();
    
    //some other code
    

    In this case, FunctionCall() doesn't consume an API. Instead it does something purely internal to the JS code such as iterating through an array and performing some logic.

    However, it is important that "some other code" executes when the function call is finished its work. Obviously in server side languages this is a given. Is this the case with JS, or will "some other code" also potentially execute if the function call isn't finished its work? If so, should I also wrap FunctionCall in a promise. If necessary, fine but I don't want every single function call to return a promise if it isn't required.

    Sorry, I'm usually used to server side languages so I'm not overly familar with the behavior of JS.

    Cheers


Comments

  • Registered Users, Registered Users 2 Posts: 1,029 ✭✭✭John_C


    If SomeService.FunctionCall(); only contains synchronous code (i.e. no promises, callbacks etc...) it will run before 'some other code'.


  • Registered Users, Registered Users 2 Posts: 1,298 ✭✭✭off.the.walls


    So in this case, some other code will be called once function call is finished doing its thing, the promise could execute at any time in down the line depends on how fast it gets its response. So your function call list could look like this

    FunctionCall()
    // some other call
    // some other function
    // Do something with data


  • Registered Users, Registered Users 2 Posts: 6,289 ✭✭✭Talisman


    The key thing to remember with JavaScript is that it is single threaded so if your code is synchronous it will execute in the order in which it was written.

    If you want to progress with NodeJS then you should understand Callbacks before diving into Promises. Promises are essentially sugar coated Callbacks.

    Kyle Simpson created a very good course which teaches the ins and outs of asynchronous programming in JavaScript.
    https://www.pluralsight.com/courses/rethinking-asynchronous-programming
    https://frontendmasters.com/courses/rethinking-async-js/

    The material covered in the course is essentially the same as that in his You Don't Know JS: Async & Performance book.

    He open sourced his series of books so the content is freely available on Github: You Don't Know JS


  • Registered Users, Registered Users 2 Posts: 9,383 ✭✭✭S.M.B.


    I started that course recently enough. Do you think it's worth continuing given that it predates async/await in JS?


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    S.M.B. wrote: »
    I started that course recently enough. Do you think it's worth continuing given that it predates async/await in JS?

    Yes you should understand the callback approach and how it works. Then when you're comfortable, learn promises and see how they fix the problem of nested callbacks for multiple requests.

    async/await has only recently been introduced and has limited support in browsers, so in all likelihood you won't be able to use in production software for the masses for a long time yet.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,298 ✭✭✭off.the.walls


    John_Mc wrote: »
    Yes you should understand the callback approach and how it works. Then when you're comfortable, learn promises and see how they fix the problem of nested callbacks for multiple requests.

    async/await has only recently been introduced and has limited support in browsers, so in all likelihood you won't be able to use in production software for the masses for a long time yet.

    This is true, if you really wanna get into in in conjunction to learning JS jump into Babel / Webpack


  • Registered Users, Registered Users 2 Posts: 6,289 ✭✭✭Talisman


    S.M.B. wrote: »
    I started that course recently enough. Do you think it's worth continuing given that it predates async/await in JS?
    It's definitely worth completing the course, it will give you an understanding of what your async/await code is actually doing.

    async/await is a sugar coated implementation of a Generators and Promises use case.

    An "async" function and the "await" keyword is a great way to write asynchronous code that waits for a single result. However you cannot wait for multiple promises at the same time.

    The material in the course goes beyond the async/await pattern and will prepare you for the future - async/await is just a stepping stone.

    Callbacks -> Promises -> Generators -> Observables -> CSP

    Communicating Sequential Processes (CSP) is part of the secret sauce baked into Go and Clojure. If you get comfortable with CSP, asynchronous code will never bother you again.


  • Registered Users, Registered Users 2 Posts: 403 ✭✭counterpointaud


    John_Mc wrote: »
    async/await has only recently been introduced and has limited support in browsers, so in all likelihood you won't be able to use in production software for the masses for a long time yet.

    Yes, but it has full support in node 8 and later, and if you have using babel (pretty common now) you can use it for browser code too. I am using it on current project since early this year and we are supporting IE11.


  • Registered Users, Registered Users 2 Posts: 9,383 ✭✭✭S.M.B.


    Talisman wrote: »
    It's definitely worth completing the course, it will give you an understanding of what your async/await code is actually doing.

    async/await is a sugar coated implementation of a Generators and Promises use case.

    An "async" function and the "await" keyword is a great way to write asynchronous code that waits for a single result. However you cannot wait for multiple promises at the same time.

    The material in the course goes beyond the async/await pattern and will prepare you for the future - async/await is just a stepping stone.

    Callbacks -> Promises -> Generators -> Observables -> CSP

    Communicating Sequential Processes (CSP) is part of the secret sauce baked into Go and Clojure. If you get comfortable with CSP, asynchronous code will never bother you again.
    This has basically confirmed my assumptions which is why I started the course.


Advertisement