Justin's Words

AsyncJS 基础

AsyncJS 指的是 async,是一个异步处理的组件。

Series 和 Limit 的区别

  • Series - the same as but runs only a single async operation at a time
  • Limit - the same as but runs a maximum of limit async operations at a time

async.map

1
2
3
4
5
6
7
8
var names = ['James', 'Kobe', 'Havard', 'Jermmy', 'Rondo'];

async.map(names, function(name, callback){
callback(null, name.toUpperCase());
}, function(err, result) {
if (err) return console.error(err);
console.log(result);
});

输出:

1
[ 'JAMES', 'KOBE', 'HAVARD', 'JERMMY', 'RONDO' ]

async. each

1
2
3
4
5
6
async.each(names, function(name, callback) {
console.log(name);
if (typeof callback === 'function') callback();
},function(err) {
console.log('done');
});

输出:

1
2
3
4
5
6
James
Kobe
Havard
Jermmy
Rondo
done

async.eachSeries

1
2
3
4
5
6
async.eachSeries(names, function(name, callback) {
console.log(name);
if (typeof callback === 'function') callback();
},function(err) {
console.log('done');
}

输出:

1
2
3
4
5
6
James
Kobe
Havard
Jermmy
Rondo
done

async.parallel

1
2
3
4
5
6
7
8
9
10
11
12
async.parallel([function(callback) {
console.log('yy');
callback();
}, function(callback) {
console.log('xx');
callback();
}, function(callback) {
console.log('dd');
callback();
}], function() {
console.log('done');
});

输出:

1
2
3
4
yy
xx
dd
done

async.waterfall

1
2
3
4
5
6
7
8
9
10
11
12
async.waterfall([function(callback) {
console.log('yy');
callback();
}, function(callback) {
console.log('xx');
callback();
}, function(callback) {
console.log('dd');
callback();
}], function() {
console.log('done');
});

输出:

1
2
3
4
yy
xx
dd
done

async.filterLimit

1
2
3
4
5
6
7
8
9
async.filterLimit(names, 2, function(name, callback) {
if (name === 'James') {
callback(name);
} else {
callback();
}
}, function(results) {
console.log(results);
});

输出:

1
[ 'James' ]

async.reject

1
2
3
4
5
6
7
8
9
async.reject(names, function(name, callback) {
if (name === 'James') {
callback(name);
} else {
callback();
}
}, function(results) {
console.log(results);
});

输出:

1
[ 'Kobe', 'Havard', 'Jermmy', 'Rondo' ]

async.reduce

1
2
3
4
5
async.reduce([1, 2, 3, 4], 0, function(memo, num, callback) {
callback(null, memo + num);
}, function(err, result) {
console.log(result);
});

输出:

1
10

async.every

1
2
3
4
5
6
7
8
9
async.every(names, function(name, callback) {
if (typeof name === 'string') {
callback(true);
} else {
callback(false);
}
}, function(result) {
console.log(result);
});

输出:

1
true

async.auto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
async.auto({
get_data: function(callback){
console.log('in get_data');
// async code to get some data
callback(null, 'data', 'converted to array');
},
make_folder: function(callback){
console.log('in make_folder');
// async code to create a directory to store a file in
// this is run at the same time as getting the data
callback(null, 'folder');
},
write_file: ['get_data', 'make_folder', function(callback, results){
console.log('in write_file', JSON.stringify(results));
// once there is some data and the directory exists,
// write the data to a file in the directory
callback(null, 'filename');
}],
email_link: ['write_file', function(callback, results){
console.log('in email_link', JSON.stringify(results));
// once the file is written let's email a link to it...
// results.write_file contains the filename returned by write_file.
callback(null, {'file':results.write_file, 'email':'user@example.com'});
}]
}, function(err, results) {
console.log('err = ', err);
console.log('results = ', results);
});

输出:

1
2
3
4
5
6
7
8
9
in get_data
in make_folder
in write_file {"get_data":["data","converted to array"],"make_folder":"folder"}
in email_link {"get_data":["data","converted to array"],"make_folder":"folder","write_file":"filename"}
err = null
results = { get_data: [ 'data', 'converted to array' ],
make_folder: 'folder',
write_file: 'filename',
email_link: { file: 'filename', email: 'user@example.com' } }