How to wait until some jQuery methods are finished?

kitlee

I am using Node.js with cheerio (a jQuery module on Node.js) and fetch (a module to get HTML body like request) to get some data from a website and save into MongoDB.

I can select the elements like jQuery in browsers

I have to get some data and the pass the data to a callback function, something like:

var data = "";
var fetchById = function(id, callback){
    var data = $("#"+id).parent().siblings(".someClass").text().replace(/\s/g, ""); // line 3
    callback(null, data);
};

and then ...

fetchById(10987, function(err, data){
    if(err){
        // errorHandle(err);
    }else{
        // db.save(data);
    }
});

But sometimes the callback is invoked before line 3 has successfully retrieved data, therefore, data is an empty string ""

How can I ensure that the callback is invoked after line 3 is finished?

Jean-Paul

You can accomplish this by explicit queueing. See jQuery API.

For example (from the API):

$( "#foo" ).slideUp();
$( "#foo" ).queue(function() {
  alert( "Animation complete." );
  $( this ).dequeue();
});

Which is the same as executing .slideUp() first and then, as a callback, executing the alert().

As for you code, this would turn into something like this:

var data = 0;

$("#some_element").queue("yourQueue", function() {
  console.log("First");

  // first function
  data = $("#"+id).parent().siblings(".someClass").text().replace(/\s/g, "");

  $(this).dequeue("yourQueue");
});

$("#some_element").queue("yourQueue", function() {
  console.log("Second");

  // second function
  callback(null, data);    // if callback() is a declared function, this should work

  $(this).dequeue("yourQueue");
});

$("#some_element").dequeue("yourQueue");

Where I used the code from this answer: How do I chain or queue custom functions using JQuery?

Important to note here, is that the .queue() function must be called on the same global element, to make sure the functions are executed (explicitly) sequential.

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 get jQuery to wait until an animation is finished?

From Dev

How can I wait until NSXMLParserDelegate methods finished?

From Dev

jQuery mobile: Wait until $.getJSON is finished

From Dev

looping for jquery load and wait until finished

From Java

How to wait until all async calls are finished

From Dev

How to wait until an animation is finished in Swift?

From Dev

How to wait until all NSOperations is finished?

From Dev

how to wait until a web request with HttpWebRequest is finished?

From Dev

How to wait until networking by Alamofire has finished?

From Dev

How to wait until my batch file is finished

From Dev

Android - How to wait until a SnapshotListener has finished?

From Dev

How to wait for animation finished in jQuery?

From Dev

Wait until function is finished

From Dev

How can I force this jquery to wait until click event has finished?

From Dev

How to wait until all tasks are finished before running code

From Dev

How to wait until all tasks finished without blocking UI thread?

From Dev

How to make python wait until the previous task is finished?

From Dev

In NodeJs how can I wait until my http get is finished?

From Dev

How to make Gulp wait until dest file is finished?

From Dev

How do I make an Excel RefreshAll wait to close until finished?

From Dev

How to make python wait until the previous task is finished?

From Dev

How to wait until an http request is finished before moving on?

From Dev

How to make JS wait until protocol execution finished

From Dev

How to make a python Script wait until download has finished

From Dev

How to wait until canvas has finished re-rendering?

From Dev

Wait until forEach loop is finished?

From Dev

Wait until gobalEval has finished

From Dev

On click myFunction and wait until it is finished

From Dev

Wait until NSURLConnection sendAsynchronousRequest is finished

Related Related

  1. 1

    How to get jQuery to wait until an animation is finished?

  2. 2

    How can I wait until NSXMLParserDelegate methods finished?

  3. 3

    jQuery mobile: Wait until $.getJSON is finished

  4. 4

    looping for jquery load and wait until finished

  5. 5

    How to wait until all async calls are finished

  6. 6

    How to wait until an animation is finished in Swift?

  7. 7

    How to wait until all NSOperations is finished?

  8. 8

    how to wait until a web request with HttpWebRequest is finished?

  9. 9

    How to wait until networking by Alamofire has finished?

  10. 10

    How to wait until my batch file is finished

  11. 11

    Android - How to wait until a SnapshotListener has finished?

  12. 12

    How to wait for animation finished in jQuery?

  13. 13

    Wait until function is finished

  14. 14

    How can I force this jquery to wait until click event has finished?

  15. 15

    How to wait until all tasks are finished before running code

  16. 16

    How to wait until all tasks finished without blocking UI thread?

  17. 17

    How to make python wait until the previous task is finished?

  18. 18

    In NodeJs how can I wait until my http get is finished?

  19. 19

    How to make Gulp wait until dest file is finished?

  20. 20

    How do I make an Excel RefreshAll wait to close until finished?

  21. 21

    How to make python wait until the previous task is finished?

  22. 22

    How to wait until an http request is finished before moving on?

  23. 23

    How to make JS wait until protocol execution finished

  24. 24

    How to make a python Script wait until download has finished

  25. 25

    How to wait until canvas has finished re-rendering?

  26. 26

    Wait until forEach loop is finished?

  27. 27

    Wait until gobalEval has finished

  28. 28

    On click myFunction and wait until it is finished

  29. 29

    Wait until NSURLConnection sendAsynchronousRequest is finished

HotTag

Archive