Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thenable support #569

Merged
merged 1 commit into from
Mar 24, 2015
Merged

Thenable support #569

merged 1 commit into from
Mar 24, 2015

Conversation

sethkinast
Copy link
Contributor

Dust handles thenables (promises) in context, either directly or as the return value of a context helper. Dust will reserve a chunk asynchronously for the eventual return value of the promise.

If the promise is invoked as a reference, the reference will become the eventual return value. If the promise is invoked as a section, the eventual return value will be pushed onto the stack.

This allows Dust to integrate with third-party modules that don't know what Dust is, while still allowing those modules to make use of Dust's asynchronous nature seamlessly.

Closes #568

@sethkinast
Copy link
Contributor Author

var q = require('q'),
    dust = require('../lib/server');

var tmpl = dust.compile('Eventually, {magic}!', 'magic');
dust.loadSource(tmpl);

dust.render('magic', { "magic": q("magic") }, function(err, out) {
  console.log(out);
});

Before:

$ node tmp/test.js
Eventually, [object Promise]!

After:

$ node tmp/test.js
Eventually, magic!

@sethkinast
Copy link
Contributor Author

Open questions:

[x] Currently a rejected promise calls chunk.setError just like any other error. However, rejected promises aren't "hard" errors. Would it make sense to render an {:error} block instead, if it exists, pushing the error onto the context?
[x] chunk.thenable isn't the coolest name ever

chunk.end(data);
}
}, function(err) {
chunk.setError(err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the {:error} idea. Let's make sure this gets into the documentation before/as this feature gets released.

@sethkinast sethkinast force-pushed the thenable branch 2 times, most recently from d6cfccf to 92f3750 Compare March 17, 2015 19:54
@sethkinast
Copy link
Contributor Author

#573 includes docs for this

},
{
name: "thenable reference",
source: "Eventually {magic}!",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would happen if someone tries to reference magic.foo. I remember we had some logic that would fire the magic function and look for foo in it's return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Thenable is not a function so it would look for a property on the Object called foo

Dust handles thenables (promises) in context, either directly or as the return value of a context helper. Dust will reserve a chunk asynchronously for the eventual return value of the promise.

If the promise is invoked as a reference, the reference will become the eventual return value. If the promise is invoked as a section, the eventual return value will be pushed onto the stack.
prashn64 added a commit that referenced this pull request Mar 24, 2015
@prashn64 prashn64 merged commit 7edf57e into linkedin:master Mar 24, 2015
@sethkinast sethkinast deleted the thenable branch March 25, 2015 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow contexts to contain Thenables
4 participants