Can I return an async value (a Promise) from a Meteor helper?

I’m working on some code that may allow helper methods to return promises, and reactively update. It’s different from simple:reactive-method in a way, in that it works with any promise type, and you invoke it by wrapping a promise-returning helper in PromiseHelper as shown:

Template.view.helpers({
  promiseHelper: PromiseHelper(function(v) {
    var s;
    s = v + ":" + textDep.get();
    return new Promise(function(resolve) {
      setTimeout(function() {
        resolve(s + ':promise');
      }, 1000 + Math.random() * 2000);
    });
  })
});

Before I do any more work on this though - is it valuable to anyone? Is it redundant to something that already exists ? Here’s the code that makes the above work: https://gist.github.com/deanius/b3ae44fca90e78a89875 - Code-specific comments welcome in the gist.

D

FYI Here’s an update that works! https://atmospherejs.com/deanius/promise

Hi @deanius I see that you’ve deprecated your package in favour of the package from okgrow. Is that package also still needed now that we have https://github.com/meteor/promise?

Hi @dcworldwide!

The MDG package does very little, and it is used by okgrow:promise, which delivers many helpers on top of it. FYI I work for OK Grow now, so that’s why the package is housed under them now :smile:

1 Like

Any chance you could give some insight into how the okgrow package actually addresses this issue? Trying to learn Meteor and Promises right now and have been working with okgrow:promise but haven’t had much success. I can use the promise methods to chain all sorts of things together and get the right results, but no matter I try, I can’t get them to actually bubble up to the template and render. Is there something else I need to do when using the Meteor.promise -> .then -> .then pattern to get the data out of the fulfilled promise?

@cafreeman - There are examples on the docs site, for example, the first one: http://okgrow-promise.meteor.com/#example-one. I’d explain more but I think the answer you are looking for is right there in the docs (see ReactivePromise wrapping the helper function?)