praekelt/vumi-jssandbox-toolkit — pull request #25 by smn (2013-08-07 19:32:56+00:00)
Title: Logging mechanism isn't returning a Promise.

Lower level it is a promise but the callback is never fired, this means that when you really need logging (something's going wrong) you're not guaranteed to see the logs.

My current hack around this is:

```javascript
self.log = function(string_or_callback) {
return function(promise_result) {
var p = im.api_request('log.info', {
'msg': (typeof string_or_callback != 'function' ?
string_or_callback : string_or_callback(promise_result))
});
p.add_callback(function() {
return promise_result;
});
return p;
};
};
```

which allows one to do the following

```javascript
var p = api.call_that_returns_a_promise();
p.add_callback(function(result) {
// handle as normal
return result;
});
// log with a function that accepts the result
p.add_callback(self.log(function(result) {
return 'you got ' + result + '!!';
}));
// log with just a string
p.add_callback(self.log('hello there'));
// add more callbacks
p.add_callback(function(result) {
// continue with result from the first callback.
});
```

This also requires the following to be added to `dummy_api.js`

```javascript
self._handle_log_info = function(cmd, reply) {
console.log(cmd['msg']);
reply(self._populate_reply(cmd, {success: true}));
}
```