0

I'm using the Firebase node module and trying to convert it's callbacks to thunks to be able to use them in Koa.

This is the original event listener callback as per the Firebase documentation:

projects.on('value', function (snapshot) {
  console.log('The read succeeded: ' + snapshot.val());
}, function (errorObject) {
  console.log('The read failed: ' + errorObject.code);
});

And this is the where I want to add it in my Koa project:

function *list() {

  // Get the data here and set it to the projects var

  this.body = yield render('list', { projects: projects });
}

Anyone know how to to do it? Have tried thunkify, thunker and thu without success...

James Moore
  • 2,501
  • 6
  • 26
  • 29
Alfred
  • 7,071
  • 4
  • 27
  • 35

1 Answers1

1

I don't think you can use thunkify etc because they are trying to convert a standard node function to a thunk. The firebase api doesn't follow the standard node.js callback signature of

fn(param1, parm2,.., function(err, result){});

which thunkify is expecting.

I think this would do it

var findProjectsByValue = function(value){
    return function(callback){
        projects.on(value, function(result){
            callback(null, result);
        }, function(err){
            callback(err);
        })            
    }
};

then you would consume it

var projects = yield findProjectsByValue('value');

Or you could just do rest api calls, which I assume is what you want. The firebase api seems to be more for evented scenarios, socketio etc

Alfred
  • 7,071
  • 4
  • 27
  • 35
James Moore
  • 2,501
  • 6
  • 26
  • 29
  • Thanks a lot! It worked perfectly. The only thing is that it's actually not a find by value but rather you can choose between listening to changes on "value", "child_added", "child_changed", "child_removed", or "child_moved". But that's just naming. – Alfred Aug 29 '14 at 22:52
  • It's worth noting that co/koa, assuming that's what you're using support Promises as well as thunks... in case they're available for the SDK/API client you are using. – Tracker1 Nov 12 '14 at 17:51