我问自己什么时候与 node.js 上的回调异步是有意义的。当我使用 I/O、数据库和其他所有阻塞或在我的其余代码中的所有内容时,它是否有用?
例如,我有一个棋盘游戏的“布局器”。它创建一个具有给定 json 布局的板实例。我知道将异步用于 I/O(读取 json 布局文件)是有意义的,因为这会阻塞。例如,foreach 循环怎么样?它应该是异步的吗?底层的 libuv 是否从中受益?
这是带有异步库的版本:
/*jshint node: true, strict: true, globalstrict: true*/
"use strict";
// Imports
var fs = require('fs');
var async = require('async');
var Figure = require('../models/figure.js');
var Field = require('../models/field.js');
var Board = require('../models/board.js');
/**
* @param {Board} board
* @param {Array} template
* @param {string} owner
* @param {function(Error)} callback
*/
var createFigures = function (board, template, owner, callback) {
async.forEach(template, function (data, done) {
var figure = new Figure(owner, data.kind);
board.setFigure(data.x, data.y, figure);
done();
}, callback);
};
/**
* @param {Board} board
* @param {Array} fields
*/
var createFields = function (board, fields, callback) {
async.series([
function(done) {
for (var x = 0; x < board.getSize().x; x++) {
for (var y = 0; y < board.getSize().y; y++) {
var field = new Field(true, false);
board.setField(x, y, field);
}
}
done();
}, function(done) {
async.forEach(fields, function(data, iteratDone) {
var field = new Field(data.passable, data.corner);
board.setField(data.x, data.y, field);
iteratDone();
});
done();
}
], callback);
};
/**
* @param {{
* ....
*/
var build = function (layoutObj, callback) {
var board = new Board(layoutObj.name, layoutObj.size.x, layoutObj.size.y);
async.parallel([
function (done) {
createFigures(board, layoutObj.figures.white, Figure.owners.white, done);
},
function (done) {
createFigures(board, layoutObj.figures.black, Figure.owners.black, done);
},
function (done) {
createFields(board, layoutObj.fields, done);
}
],
function (err) {
callback(err, board);
}
);
};
/**
* @param string layoutPath
* @param {function(Error, )} callback
* @return {Board}
*/
var generateBoardWithLayout = function (layoutPath, callback) {
async.waterfall([
function(done) {
fs.stat(layoutPath, function(err, stats) {
done(null, stats);
});
}, function(stats, done) {
if (stats.isFile()) {
var jsonData = '';
fs.readFile(layoutPath, function(err, data) {
jsonData += data;
done(null, jsonData);
});
} else {
done(new Error("There is no '" + layoutPath + "'"));
}
}
], function(err, jsonData) {
build(JSON.parse(jsonData), callback);
});
};
module.exports.build = build;
module.exports.generateBoardWithLayout = generateBoardWithLayout;
这就是没有异步链接的版本
/*jshint node: true, strict: true, globalstrict: true*/
"use strict";
// Imports
var Figure = require('../models/figure.js');
var Field = require('../models/field.js');
var Board = require('../models/board.js');
/**
* @constructor
*/
function Layouter() {
}
Layouter.generateBoardWithLayout = function(layoutPath) {
var fs = require('fs');
var jsonData = fs.readFileSync(layoutPath);
if (!jsonData) {
throw new Error("There is no '" + layoutPath + "'");
}
return new Layouter().build(JSON.parse(jsonData));
};
Layouter.prototype = {
/**
* @param {{
* ....
*/
build: function (layoutObj) {
var board = new Board(layoutObj.name, layoutObj.size.x, layoutObj.size.y);
this.createFigures_(board, layoutObj.figures.white, Figure.owners.white);
this.createFigures_(board, layoutObj.figures.black, Figure.owners.black);
this.createFields_(board, layoutObj.fields);
return board;
},
/**
* @param {Board} board
* @param {Array} template
* @param {string} owner
* @private
*/
createFigures_: function (board, template, owner) {
template.forEach(function (data) {
var figure = new Figure(owner, data.kind);
board.setFigure(data.x, data.y, figure);
});
},
/**
* @param {Board} board
* @param {Array} fields
* @private
*/
createFields_: function (board, fields) {
for (var x = 0; x < board.getSize().x; x++) {
for (var y = 0; y < board.getSize().y; y++) {
var field = new Field(true, false);
board.setField(x, y, field);
}
}
fields.forEach(function (data) {
var field = new Field(data.passable, data.corner);
board.setField(data.x, data.y, field);
});
}
};
module.exports = Layouter;
module.exports.generateBoardWithLayout = Layouter.generateBoardWithLayout;
谢谢你的时间!
问候,马库斯