Why and how is dust.js asynchronous
Today we had a discussion at work about which templating library we should use for our backbone applications. Since Dust is the library most teams in the company use, we thought it may be the direction we want to take. I have never used Dust, but some people in the room mentioned that the fact that it was asynchronous made it a little painful to do some tasks. It was very weird for me to hear that the rendering of templates happened asynchronously, mostly because I don’t know a way to make JS execute asynchronously other than using setTimeout. So I decided to dive into the code and figure out how they are doing it.
I started my journey by getting dust from github. The only file I really needed was dist/dust-full-0.3.0.js, so I got the file and built a simple example in an HTML file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html>
<head>
<script src='dust-full-0.3.0.js'></script>
<script>
var compiled = dust.compile("Hello {name}!", "intro");
dust.loadSource(compiled);
dust.render("intro", {name: "Fred"}, function(err, out) {
console.log(out);
});
</script>
</head>
<body>
</body>
</html>