2

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?

1
  • Dust 2.7.2 is out with the fix. Commented Jun 8, 2015 at 21:50

2 Answers 2

2

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}
Sign up to request clarification or add additional context in comments.

Comments

0

Turns out you have to loop through itself...

{#async}
    {#.}
        {name}
    {/.}
{/async}

2 Comments

This seems like a bug; I opened an issue for you on our issue tracker: github.com/linkedin/dustjs/issues/674
This will be fixed in 2.7.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.