I was wondering wich of the following is the right thing to do, or which one is faster?
I have a function which generates a HTML string and gives it back.
How should I return this string that I generate, with a callback or just return it?
MyApp.prototype.makeChatMsg = function(message, callback) {
var html = '<div class="media">';
html += '<div class="media-body">';
//html += '<h4 class="media-heading">' + message.user.first_name + ' ' + message.user.last_name + '</h4>';
html += '<p>' + message.content + '</p>';
html += '</div>';
html += '</div>';
callback(html); // see here
}
vs
MyApp.prototype.makeChatMsg = function(message) {
var html = '<div class="media">';
html += '<div class="media-body">';
//html += '<h4 class="media-heading">' + message.user.first_name + ' ' + message.user.last_name + '</h4>';
html += '<p>' + message.content + '</p>';
html += '</div>';
html += '</div>';
return html; // see here
}
Im using these functions like this within MyApp scope
First method
this.makeChatMsg(incomingMessage, function(html) {
$('#messages').append(html);
));
Second method
var newMessage = this.makeChatMsg(incomingMessage);
$('#messages').append(newMessage);
Generating a HTML string is this a asynchronous task or not? Im just not sure..