JavaScript Callback function clarification

I have a javascript callback function setup like this

function myFunction(callback){

    $('meta[name="viewport"]').prop('content', 'width=1440');
    //other code
    callback()
}

function otherFunction(){
   //some async operation
}

myFunction(otherFunction)

Does this code ensure that my otherFunction does not run before the viewport is changed and other code above it is implemented?

Answer:1

If all the code above the callback call is synchronous, then yes, otherFunction will run after all the code above.

Answer:2

Asynchronous callback functions will always run after all synchronous code has been completed. This is because JS has something called the event loop which looks the following way:

enter image description here

Whenever we do asynchronous operation often this is associated with a Web API. This web API is abstracted away from us. We don't need to know how the browser implements this feature (which probably is written in C++ as part of the browser). However, we are interested in the value of operation or eventual return value.

So what the browser does is whenever the webAPI's are finished the callback associated with it is pushed in the callback queue. Then whenever the stack is empty the event loop will push an function on the stack.

This is why asynchronous operations will always be performed last, because the event loop will wait for the stack to be empty before pushing an item on it.

Answer:3

Here is best way to look into it. Here i have added a statement just before the callBack function . You can see the console log even after otherfunction execution. So it entirely depends upon whether you are using synchronous or asynchronous code before the function invocation.

function myFunction(callback){

    $('#ipt').val(callback.name);
    //other code
    setTimeout(function(){console.log('hello')},5000)
    callback()
}

function otherFunction(){
   alert( $('#ipt').val());
}

myFunction(otherFunction)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="ipt" placeholder="Dummy text"/>
Answer:4

I have a page with a hidden form where a value is echoed by php. With javascript/jQuery I pick up the value and store it in a cookie. The user is redirected to an external page, then is redirected ...

I have a page with a hidden form where a value is echoed by php. With javascript/jQuery I pick up the value and store it in a cookie. The user is redirected to an external page, then is redirected ...

  1. javascript cookie value read
  2. javascript cookie value
  3. javascript cookie value get
  4. javascript cookie value change
  5. javascript cookie value update
  6. javascript cookie value in php
  7. javascript get cookie value by name
  8. javascript set cookie value
  9. javascript check cookie value
  10. javascript read cookie value by name
  11. javascript print cookie value
  12. javascript edit cookie value
  13. javascript add cookie value
  14. javascript alert cookie value
  15. javascript fetch cookie value
  16. javascript display cookie value
  17. javascript write cookie value
  18. javascript access cookie value
  19. javascript retrieve cookie value
  20. javascript replace cookie value

I have a dist folder containing CSS, fonts, JS folder and an index.html file minimized for Vue.js, ready to deploy and use. I want to use Node.js to run this application. How can I set this up to just ...

I have a dist folder containing CSS, fonts, JS folder and an index.html file minimized for Vue.js, ready to deploy and use. I want to use Node.js to run this application. How can I set this up to just ...

  1. deploy application node.js
  2. deploy app nodejs
  3. deploy node application
  4. how to deploy node js application
  5. deploy node application on server
  6. deploy node application to aws
  7. deploy node application to heroku
  8. deploy node application on iis
  9. deploy node application to docker
  10. deploy node application to azure
  11. deploy node application on tomcat
  12. deploy node application with jenkins
  13. deploy node application on windows
  14. deploy node js application on azure
  15. deploy node application to windows server
  16. deploy node application using jenkins
  17. deploy node js application cloud
  18. deploy node js application bitnami
  19. deploy node application npm
  20. deploy node app to heroku

I have placed a csv file in my assets folder for my react app, however, that file is not getting picked up and added to my dist build via webpack (the images are still added as assets to the build but ...

I have placed a csv file in my assets folder for my react app, however, that file is not getting picked up and added to my dist build via webpack (the images are still added as assets to the build but ...

  1. add webpack file
  2. add file to webpack build
  3. add config file webpack

I have an array with objects of unknown depth, like this var objects = [{ id: 1, name: 'foo' }, { id: 2, name: 'bar', childs: [{ id: 3, name: 'baz', childs:...

I have an array with objects of unknown depth, like this var objects = [{ id: 1, name: 'foo' }, { id: 2, name: 'bar', childs: [{ id: 3, name: 'baz', childs:...

  1. lodash recursive filter