Reputation: 67
My callback function in 'async.series' returns several values and creates several outputs from 'elements'.
How can I write the return values into an array by using 'forEach'?
async.series(
{
// Browse
elements: function(callback) {
Server_browse(item, function(result) {
callback(null,result);
});
},
},
function(err,result) {
if (err) {
console.log(" error : ", err);
console.log(err.stack);
}
console.log('elements=' + JSON.stringify(result.elements));
}
);
Upvotes: 1
Views: 766
Reputation: 67
Here are the other function 'browse' and Server_browse.
The function 'Server_browse' consigns an Folder or Variable to the function 'browse'. In 'browse' i will added the Variable in an array or call 'Server_browse' again, if the Item a folder. Current I get all Folders and Variables seperate on 'console.log(result.elements)'. But i willl write all folders and variables in an Array.
var browse = function(item,indx,array,callback){
// 'item' is a Folder or Variable which i get from the server
//item = {"MyVariable1":{"referenceTypeId":"ns=0;i=46","isForward":true,"nodeId":"ns=1;i=1005","browseName":{"namespaceIndex":0,"name":"MyVariable1"},"displayName":{"text":"MyVariable1"},"nodeClass":"Variable","typeDefinition":"ns=0;i=63"}};
var child = ns='+item.browseName.namespaceIndex+';i='+item.nodeId.value;
if (item.$nodeClass.key == 'Variable') {
callback(item);
}else{
Server_browse(child,function(result){
callback(result);
});
}
}
var Server_browse = function(item,callback){
node_browse.session.browse( item,function (err, itemResults,diagnostics) {
if (err) {
console.log(err);
console.log(itemResults);
console.log(diagnostics);
}else{
for(var i=0; i<itemResults.length; i++){
itemResults[i].references.forEach(function(element,index,arr){
browse(element, index, arr, function(item){
callback(item);
});
});
}
}
});
}
Upvotes: 0
Reputation: 5224
There is no use for caolan/async if you are only making one request.
Where does you item
variable comes from in Server_browse(item, ...
?
I may be wrong, because there is missing info, but I think you want to achieve this:
var items = ['query1', 'query2', 'query3'];
async.mapSeries(
items,
// for each item
function(item, callback) {
Server_browse(item, function(result) {
callback(null, result.elements);
});
},
// when all queries are done
function(error, resultElements) {
console.log(resultElements)
}
);
Upvotes: 1