Vyacheslav
Vyacheslav

Reputation: 27221

Asynchronous concating strings in nodejs

What is the best solution to concate lots of strings? I wanna get a lot of information from my mongodb database and send via email. I have a lot of data to concate. The usual way is:

var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);

I have some array:

var strs = {'str0','str1','str2',.....};

I need something like this:

asyncConcate(strs,function (result)
{
console.log(result);
});

Upvotes: 0

Views: 68

Answers (1)

azero0
azero0

Reputation: 2310

try this code and see if it helps:

(used node.js async module https://github.com/caolan/async)

var async = require("async");
var finalResult = "";
var strArray = ["str1","str2","str3"];
var limit = strArray.length-1;

async.forEachLimit(strArray,limit,function(item,callback){
    finalResult+=item;
    callback(); 
});
console.log(finalResult);

Upvotes: 2

Related Questions