question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

async function in the $.each callbacks

See original GitHub issue

I have following code in my node script.

$('p').each(function (idx, elem) {
  var article = new Article({content: $(this).html()});
  article.save(function (err) {// do something}); // db save async function or other async function
});

mongoose.connection.close();

However, the mongoose.connection.close() will run before all the documents are saved to the database. So how can I make sure all things are done before closing the db connection?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
felixfbeckercommented, Sep 16, 2015

This is a problem with async code in general. Mongoose has support for promises, so you could do this:

var promises = [];
$('p').each(function (idx, elem) {
  var article = new Article({content: $(this).html()});
  var promise = article.save();
  promises.push(promise); // db save async function or other async function
});
Promise.all(promises)
  .then(doSomethingOnSuccess)
  .catch(doSomethingOnError)
  .finally(mongoose.connection.close);
5reactions
fb55commented, Jan 19, 2022

Cheerio now supports iterators, so the modern way of doing this would be:

for (const elem of $('p')) {
  const article = new Article({content: $(elem).html()});
  try {
    // db save async function or other async function
    await article.save();
  } catch(err) {
    // do something with `err`
  }
}

await mongoose.connection.close();
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Handle Async Callbacks in JavaScript...Without ...
Using callbacks, we can begin to separate – with a callback, we can load the data and, when the asynchronous operation is done,...
Read more >
Callback Functions | Async-Await | Promises - Enlear Academy
Await eliminates the use of callbacks in .then() and .catch(). In using async and await, async is prepended when returning a promise, await...
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
The key takeaway here is that callback functions are not asynchronous— setTimeout is the asynchronous Web API responsible for handling ...
Read more >
Introducing asynchronous JavaScript - Learn web development
An event handler is a particular type of callback. A callback is just a function that's passed into another function, with the expectation...
Read more >
Getting to know asynchronous JavaScript: Callbacks ... - Medium
The process of wrapping a callback based asynchronous function inside a Promise and return that promise instead is called “promisification”. We ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found