Async Mindset Snippets
nodejs async module snippets designed to improve your coding style
Details
Installs
- Total 797
- Win 459
- Mac 203
- Linux 135
Nov 21 | Nov 20 | Nov 19 | Nov 18 | Nov 17 | Nov 16 | Nov 15 | Nov 14 | Nov 13 | Nov 12 | Nov 11 | Nov 10 | Nov 9 | Nov 8 | Nov 7 | Nov 6 | Nov 5 | Nov 4 | Nov 3 | Nov 2 | Nov 1 | Oct 31 | Oct 30 | Oct 29 | Oct 28 | Oct 27 | Oct 26 | Oct 25 | Oct 24 | Oct 23 | Oct 22 | Oct 21 | Oct 20 | Oct 19 | Oct 18 | Oct 17 | Oct 16 | Oct 15 | Oct 14 | Oct 13 | Oct 12 | Oct 11 | Oct 10 | Oct 9 | Oct 8 | Oct 7 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Windows | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
Mac | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Linux | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
Readme
- Source
- raw.githubusercontent.com
Examples
series TAB ->
return async.series({
nameOfStepOne: function(callback) {
// After naming step one and tabbing again, you are here
}
});
step TAB ->
return async.series({
stepOne: function(callback) {
...
},
stepTwo: function(callback) {
// Position yourself after step one, type "step"
// and hit tab; you are prompted to name "stepTwo".
// Hit tab again and you're here
}
});
**each TAB ->
**
“javascript
return async.eachSeries(myArray, function(item, callback) {
// After typing myArray and pressing tab again, you are here
}, callback);
**`err TAB ->`**
```javascript
if (err) {
return callback(err);
}
**done TAB ->
**
”javascript
return callback(null);
**`skip TAB ->`**
```javascript
// Safely invoke callback when we didn't
// do anything async
return setImmediate(callback);
**skipr TAB ->
**
“javascript
// Safely invoke callback with some data when we
// didn't anything async
return setImmediate(_.partial(callback, null, data));
**`whil TAB ->`**
```javascript
var done = false;
return async.whilst(
function() { return !done; },
function(callback) {
// You start typing again here
}, callback
);
Your inner function keeps getting called until you set done
to true.
Philosophy
If you're gonna code with callbacks, you gotta use the async module. Otherwise you're writing spaghetti code and infinitely nested curly braces.
The async module solves this problem, but it takes a lot of typing and that leads to mistakes. That's where these snippets come in.
These snippets have aggressively short keywords to make it easy to drill them into your fingers and improve your node programming style permanently.
All of these snippets start with return
to avoid accidentally falling through to other code. Calling another function that takes a callback does not stop the execution of your function unles you return.
The each
snippet deliberately uses eachSeries
. async.each
is usually a mistake because you haven't thought about the consequences of (100? 1,000? 1,000,000?) things happening at once.
Switch your code to eachLimit
later if you have a rational reason to believe you can handle more than one at a time safely.
You should always use skip
if you have not done any asynchronous work yet in your function and you need to invoke a callback asynchronously to avoid crashing the JavaScript stack. If you don't use skip
(which calls setImmediate
), your code will seem fine until one day you have a few hundred items and the JavaScript stack crashes. This is especially true with async.eachSeries
.
Credits
These snippets were developed to support our work at P'unk Avenue.