NodeJS:如何使用 async.js 处理数据库中的项目列表
NodeJS: How to use async.js to process a list of items in database
我遇到了这个问题:我从 API 中得到了一个项目列表,我必须将它们保存在数据库中。对于它们中的每一个,我都必须显示一些反馈,例如“-项目名称。保存在数据库中..”并在保存后显示类似 "Sucess! Next...".
的内容
我正在使用异步 link 遍历数组中的项目并保存在数据库中。问题是消息不同步。它显示了一堆项目名称消息和一堆成功消息之后。我的代码如下。
var saveInDB = function saveInDB(item, callback) {
console.log(' - ' + item.market_name + ' .......');
// Simulating some delay
setTimeout(function () {
console.log('Success! Next....');
callback(null);
}, 3000);
}
util.getJsonFromUrl(API_URL, function (err, data) {
if (err || !data || !data.results || data.results.length == 0)
{
console.log('BAD');
}
else
{
console.log('Items downloaded...');
async.each(
data.results,
function (item, callback) {
saveInDB(item, callback);
},
function (err) {
if (err) {
console.log('ERROR');
}
else {
console.log('Success!');
}
}
);
}
});
是的,我曾尝试使用 .series,但就是无法正常工作。我一直在寻找示例,但我仍然不知道如何像 .each.
中那样简单地传递项目
你能帮帮我吗?在此先感谢您的帮助。
async.each(data.results, function (result, cb_results) {
//try your here
cb_results();
}, function (err) {
if (err) { throw err; }
});
示例:
var returnData = [];
async.each(data.results, function (result, cb_results) {
var myJson = {};
myJson.id = result.id;
................
returnData.push(myJson);
cb_results();
}, function (err) {
if (err) { throw err; }
callback(returnData);
});
基于异步 documentation 使用 async.each
Applies the function iteratee to each item in arr, in parallel.
看起来他们甚至警告说这不会保留顺序,因为它是并行的:
Note, that since this function applies iteratee to each item in
parallel, there is no guarantee that the iteratee functions will
complete in order.
如果我没理解错的话,您希望您的项目(假设您有 A、B、C)按以下顺序执行:
//This uses async.eachSeries
For each - A
SaveInDB called.
Timeout set. Waiting to timeout ...
Processing DB transaction for > A ....... Done.
Success! Next....
For each - B
SaveInDB called.
Timeout set. Waiting to timeout ...
Processing DB transaction for > B ....... Done.
Success! Next....
For each - C
SaveInDB called.
Timeout set. Waiting to timeout ...
Processing DB transaction for > C ....... Done.
Success! Next....
并且不是按以下顺序(或者可能不同,它是并行的,不保证顺序):
//uses async.each
For each - A
SaveInDB called.
Timeout set. Waiting to timeout ...
For each - B
SaveInDB called.
Timeout set. Waiting to timeout ...
For each - C
SaveInDB called.
Timeout set. Waiting to timeout ...
Processing DB transaction for > A ....... Done.
Success! Next....
Processing DB transaction for > B ....... Done.
Success! Next....
Processing DB transaction for > C ....... Done.
Success! Next....
因此,请将您的方法从 async.each
更改为 async.eachSeries
,并确保您不会感到困惑,请参阅下面的 saveInDB
函数。
function saveInDB(item, callback) {
//this will not wait for timeout.
console.log('SaveInDB called.');
// Simulating some delay
setTimeout(function () {
//everything in this block waits for timeout.
console.log('Processing DB transaction for > ' + item + ' ....... Done.');
console.log('Success! Next....');
callback(null)
}, 5000);
//this will not wait for timeout
console.log('Timeout set. Waiting to timeout ...');
}
我会尝试使用真实数据库,而不是使用超时模拟。小心超时,请参阅下面来自 documentation 的内容:
It is important to note that your callback will probably not be called
in exactly delay milliseconds - Node.js makes no guarantees about the
exact timing of when the callback will fire, nor of the ordering
things will fire in. The callback will be called as close as possible
to the time specified.
我遇到了这个问题:我从 API 中得到了一个项目列表,我必须将它们保存在数据库中。对于它们中的每一个,我都必须显示一些反馈,例如“-项目名称。保存在数据库中..”并在保存后显示类似 "Sucess! Next...".
的内容我正在使用异步 link 遍历数组中的项目并保存在数据库中。问题是消息不同步。它显示了一堆项目名称消息和一堆成功消息之后。我的代码如下。
var saveInDB = function saveInDB(item, callback) {
console.log(' - ' + item.market_name + ' .......');
// Simulating some delay
setTimeout(function () {
console.log('Success! Next....');
callback(null);
}, 3000);
}
util.getJsonFromUrl(API_URL, function (err, data) {
if (err || !data || !data.results || data.results.length == 0)
{
console.log('BAD');
}
else
{
console.log('Items downloaded...');
async.each(
data.results,
function (item, callback) {
saveInDB(item, callback);
},
function (err) {
if (err) {
console.log('ERROR');
}
else {
console.log('Success!');
}
}
);
}
});
是的,我曾尝试使用 .series,但就是无法正常工作。我一直在寻找示例,但我仍然不知道如何像 .each.
中那样简单地传递项目你能帮帮我吗?在此先感谢您的帮助。
async.each(data.results, function (result, cb_results) {
//try your here
cb_results();
}, function (err) {
if (err) { throw err; }
});
示例:
var returnData = [];
async.each(data.results, function (result, cb_results) {
var myJson = {};
myJson.id = result.id;
................
returnData.push(myJson);
cb_results();
}, function (err) {
if (err) { throw err; }
callback(returnData);
});
基于异步 documentation 使用 async.each
Applies the function iteratee to each item in arr, in parallel.
看起来他们甚至警告说这不会保留顺序,因为它是并行的:
Note, that since this function applies iteratee to each item in parallel, there is no guarantee that the iteratee functions will complete in order.
如果我没理解错的话,您希望您的项目(假设您有 A、B、C)按以下顺序执行:
//This uses async.eachSeries
For each - A
SaveInDB called.
Timeout set. Waiting to timeout ...
Processing DB transaction for > A ....... Done.
Success! Next....
For each - B
SaveInDB called.
Timeout set. Waiting to timeout ...
Processing DB transaction for > B ....... Done.
Success! Next....
For each - C
SaveInDB called.
Timeout set. Waiting to timeout ...
Processing DB transaction for > C ....... Done.
Success! Next....
并且不是按以下顺序(或者可能不同,它是并行的,不保证顺序):
//uses async.each
For each - A
SaveInDB called.
Timeout set. Waiting to timeout ...
For each - B
SaveInDB called.
Timeout set. Waiting to timeout ...
For each - C
SaveInDB called.
Timeout set. Waiting to timeout ...
Processing DB transaction for > A ....... Done.
Success! Next....
Processing DB transaction for > B ....... Done.
Success! Next....
Processing DB transaction for > C ....... Done.
Success! Next....
因此,请将您的方法从 async.each
更改为 async.eachSeries
,并确保您不会感到困惑,请参阅下面的 saveInDB
函数。
function saveInDB(item, callback) {
//this will not wait for timeout.
console.log('SaveInDB called.');
// Simulating some delay
setTimeout(function () {
//everything in this block waits for timeout.
console.log('Processing DB transaction for > ' + item + ' ....... Done.');
console.log('Success! Next....');
callback(null)
}, 5000);
//this will not wait for timeout
console.log('Timeout set. Waiting to timeout ...');
}
我会尝试使用真实数据库,而不是使用超时模拟。小心超时,请参阅下面来自 documentation 的内容:
It is important to note that your callback will probably not be called in exactly delay milliseconds - Node.js makes no guarantees about the exact timing of when the callback will fire, nor of the ordering things will fire in. The callback will be called as close as possible to the time specified.