How would I denodeify this npm package

I am attempting to use the npm activitystrea.ms package to ease and perhaps standardize a bit the building of activity records in my app.

My first attempt failed because I tried to do a database insert inside of a callback that is in a method at which point meteor complained that meteor functions were being executed outside of a fiber. That code looked like:

    as.create().
      actor(as.person().id(group.members[0].userId)).
      object(as.group().id(groupId).name(group.name)).
      startTimeNow().
      get().
      export((err, doc) => { Activities.insert(doc); });

After a little research, I found that I needed to use bindEnvironment. So, the code now looks like:

    const boundFunction = Meteor.bindEnvironment((err, doc) => {
      Activities.insert(doc);
    }, function (e) {throw e;});

    as.create().
      actor(as.person().id(group.members[0].userId)).
      object(as.group().id(groupId).name(group.name)).
      startTimeNow().
      get().
      export(boundFunction);

This works, but doesn’t seem very clean and I’m going to be doing this throughout my methods.

I’ve read that promises automagically use fibers and avoid the untidyness of the bindEnvironment approach. But, the examples of Promise.denodeify that I’ve found don’t seem to address how to fix the export function in the above example so that it will take an ActivityStream object and return a promise.

My goal would be to be able to do something like this in my methods:

    as.create().
      actor(as.person().id(group.members[0].userId)).
      object(as.group().id(groupId).name(group.name)).
      startTimeNow().
      get().
      exportPromise().
      then(function(doc) { Activities.insert(doc); });

How would this be done? Would I have to subclass “as” perhaps?

(Note that I’m using the “wip-upgrade-to-node4” branch of meteor if that makes a difference… I think the promise implementation is changing).

So I gave in and tried to fork “activitystrea.ms”. This is my first attempt at actually attempting to modify an npm package used in my Meteor project. I figured, how hard could it be to promisify the export routine directly?

Well, the first question was “how do I make promises available to it?” The meteor promise package actually uses an npm package titled “meteor-promise” under the hood so it is already present. But I can’t see how to use the promise package that meteor is already bringing in, so I ended up doing a "meteor npm install --save ‘meteor-promise’ to get another copy in.

With that, I was able to get the code to work… right up until the doh moment when I realized this meteor-promise is not tied into “fibers” like the other one and I’m back to the error about everything being required to run in fibers.

So, I followed the directions, ran “meteor npm install --save fibers” and linked the promise package to the fibers package in the code. Still no go. The fibers package throws. Suspect two fibers in one project isn’t good? Or there is some way they have to be coordinated, initialized or something?

Which brings me to a big question… if we’re switching to npm packages, where are all the examples of how to make calls to the meteor api from within these packages? It’s not terribly common for me to want to create a package that doesn’t need to tie into the meteor api at some point, especially with this fibers issue in the way.