Reputation: 1106
I have an issue with promises that resolve an array in Dust.js
Say I have a Dust.js function like so (this will be doing something else later on but for sake of a simple example), which resolves an array:
var doSomethingAsync = new Promise(function(resolve, reject) {
// do a thing, possibly async, then…
setTimeout(function(){
var items = [{
name: 'One',
}, {
name: 'Two',
}, {
name: 'Three',
}];
resolve(items);
}, 5000);
});
res.stream('admin/index', {
"async": doSomethingAsync
});
and my template is like this:
{#async}
{name}
{/async}
It doesn't seem to print out what you would expect. Using contextDump helper it prints this:
[ { "name": "One" }, { "name": "Two" }, { "name": "Three" } ]
Anyone know what I'm doing wrong, whether this is intended behaviour or whether it is a bug?
Upvotes: 2
Views: 261
Reputation: 17434
Your answer is correct. Currently the entire Array gets pushed onto the context, so you then have to iterate over your context:
{#async}
{#.}
{name}
{/.}
{/async}
But this is fixed in Dust 2.7.2.
Using that version, you can iterate over the returned Array just as you'd expect.
{#async}
{name}
{/async}
Upvotes: 2
Reputation: 1106
Turns out you have to loop through itself...
{#async}
{#.}
{name}
{/.}
{/async}
Upvotes: 0