Closed jbenet closed 10 years ago
Done, now all async.
Damn. switching to conversions broke the nice api. can no longer do:
var transformer = require('dat-transformer');
var iso2js = transformer('iso-date', 'js-date');
console.log(iso2js('2014-04-26T12:02:23.000Z'));
Now have to do
var transformer = require('dat-transformer');
var iso2js = transformer('iso-date', 'js-date');
return iso2js('2014-04-26T12:02:23.000Z', function(err, out) {
console.log(out);
});
Which really sucks for simple things. I may not be the best thing to force all conversions to be async. The problem is some have to, and detecting + composing sync + async correctly will get complicated.
Maybe promises is the right thing to do here (ew).
Or having an optimistic sync
call that only resolves sync conversions.
Basically, make both of these possible:
var transformer = require('dat-transformer');
var iso2js = transformer.sync('iso-date', 'js-date');
console.log(iso2js('2014-04-26T12:02:23.000Z'));
var transformer = require('dat-transformer');
var iso2js = transformer.async('iso-date', 'js-date');
iso2js('2014-04-26T12:02:23.000Z', function(err, out) {
console.log(out);
});
Ok, so this interface now works. Supports:
transformer.sync
and transformer.async
(transformer
defaults to sync
.)process.nextTick
) callbacks since users won't.callback(err, output)
signaturetransformer.{sync,async}.compose
(tricky in async context, as conversion and callback signatures differ: conversion(input, callback)
and callback(err, output)
).Error: async conversion foo-to-bar should take 2 args (input, callback):
function convert(foo) {
return 'bar';
}
Error: sync conversion foo-to-bar should take 1 arg (input):
function convert(foo, callback) {
return 'bar';
}
(thanks for the help @mafintosh @maxogden @groundwater!)
:+1: glad to have helped
Some conversions will be async, for example
us-street-address-to-us-city
will use node-geocoder. The Api needs to move fromto
(Unless fibers are used).