folktale / data.task

Migrating to https://github.com/origamitower/folktale
MIT License
425 stars 45 forks source link

Provide the `Task#taskify` helper function #30

Closed iliocatallo closed 8 years ago

iliocatallo commented 8 years ago

Hi,

It would be really convenient to augment Task so as to include two additional helper methods: Task#taskify and Task#taskifyAll (similarly to what provided by Bluebird for promises). This would allow avoiding manual wrapping such as:

let connectToMongoDb = mongoUri => new Task((reject, result) => {
    MongoClient.connect(mongoUri, (err, db) => {
        if (err) {
            return reject(err);
        }
        result(db);
    });
});

for each single async. function in the codebase.

Thank you!

robotlolita commented 8 years ago

Task#taskify is already provided by @stoeffel's Futurize library (https://github.com/stoeffel/futurize) :3

I'm particularly against the concept of Task#taskifyAll (and Promise#promisifyAll, for that matter), since it creates APIs that are not supposed to exist, e.g.:

var events = new EventEmitter();
Task.taskifyAll(events);
events.onAsync(function(){ ... }); // what does this even mean?

If you still want to write it, you can implement it as:

var futurize = require('futurize')(require('data.task'));

function taskifyAll(o) {
  Object.keys(o).forEach(function(key) {
    o[key + 'Async'] = futurize(o[key])
  });
  return o;
}
iliocatallo commented 8 years ago

Hi,

thank you for your prompt answer. I understand your concern about the possibility of creating non-sensical APIs. On the other hand, one could argue that programming is inherently an unsafe activity, and that - in this specific case - the price to pay for defending the developer from herself is way too high to be acceptable.

Thank you again for your great library.