async function in the $.each callbacks
See original GitHub issueI 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:
- Created 8 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is a problem with async code in general. Mongoose has support for promises, so you could do this:
Cheerio now supports iterators, so the modern way of doing this would be: