Run one function after another: callback issue

A. Z.

I'm trying to understand why function lazy_2 doesn't run this way:

function lazy_1 (callback) {
    alert('lazy_1');
}

function lazy_2 () {
    alert('lazy_2');   
}

lazy_1(lazy_2);

? (Via Execute jquery function after another function completes.)

jQuery method works well:

function lazy_1 (callback) {
    alert('lazy_1');
}

function lazy_2 () {
    alert('lazy_2');   
}

$.when( lazy_1() ).done(function() {
    lazy_2();
});

http://jsfiddle.net/5LL69/

jfriend00

Because lazy_1() doesn't call it's callback - in fact nobody does. It needs to look like this for the callback to get called:

function lazy_1 (callback) {
    alert('lazy_1');
    callback();
}

function lazy_2 () {
    alert('lazy_2');   
}

lazy_1(lazy_2);

Your second code block above is equivalent to:

lazy1();
lazy2();

because you're just asking jQuery $.when() to run one function and then another with no promises involved (all synchronous code).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to run function after another one completes

From Dev

How to make a javascript function to run after another one get finished?

From Dev

Run another task after executing one function successfully Using Celery

From Dev

Calling a click function after a JSON callback issue

From Dev

Run a callback after multiple function have completed

From Dev

How can I run one gulp function after another in a javascript function?

From Dev

Elixir: Run function after another function

From Dev

Callback function / Run a function after adding a class with .addClass

From Dev

Run one rake task after another completes

From Dev

Run a Program After Another in One Java File

From Dev

Run multiple scripts one after another in bash

From Dev

RethinkDB - Run query one after another

From Dev

Make two functions run one after the other using callback

From Dev

Run a function only when another one is finished

From Dev

Issue with downloadPage callback function

From Dev

Issue with downloadPage callback function

From Dev

How do you make a code run after a callback function in Angular?

From Dev

Mocking another parameter on function not working when one of them is a callback interface

From Dev

How to run grep at one file in another avoiding memory exaust issue?

From Dev

Running one function after another completes

From Dev

Alternative ways to call a function after another one

From Dev

How to display output of one function after another

From Dev

WebGL- Issue when rendering one shape after another

From Dev

Callback function after an assignment

From Dev

issue getting proper promise failed function to run after broken promise

From Dev

Run function after one function is complete - JQuery animate

From Dev

how to run multiple jobs one after another in jenkins

From Dev

How to run multiple python file in a folder one after another

From Dev

add SKAction to Sprite queue run one after another

Related Related

  1. 1

    how to run function after another one completes

  2. 2

    How to make a javascript function to run after another one get finished?

  3. 3

    Run another task after executing one function successfully Using Celery

  4. 4

    Calling a click function after a JSON callback issue

  5. 5

    Run a callback after multiple function have completed

  6. 6

    How can I run one gulp function after another in a javascript function?

  7. 7

    Elixir: Run function after another function

  8. 8

    Callback function / Run a function after adding a class with .addClass

  9. 9

    Run one rake task after another completes

  10. 10

    Run a Program After Another in One Java File

  11. 11

    Run multiple scripts one after another in bash

  12. 12

    RethinkDB - Run query one after another

  13. 13

    Make two functions run one after the other using callback

  14. 14

    Run a function only when another one is finished

  15. 15

    Issue with downloadPage callback function

  16. 16

    Issue with downloadPage callback function

  17. 17

    How do you make a code run after a callback function in Angular?

  18. 18

    Mocking another parameter on function not working when one of them is a callback interface

  19. 19

    How to run grep at one file in another avoiding memory exaust issue?

  20. 20

    Running one function after another completes

  21. 21

    Alternative ways to call a function after another one

  22. 22

    How to display output of one function after another

  23. 23

    WebGL- Issue when rendering one shape after another

  24. 24

    Callback function after an assignment

  25. 25

    issue getting proper promise failed function to run after broken promise

  26. 26

    Run function after one function is complete - JQuery animate

  27. 27

    how to run multiple jobs one after another in jenkins

  28. 28

    How to run multiple python file in a folder one after another

  29. 29

    add SKAction to Sprite queue run one after another

HotTag

Archive