Skip to content

Instantly share code, notes, and snippets.

@aseemk
Created April 10, 2011 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aseemk/912279 to your computer and use it in GitHub Desktop.
Save aseemk/912279 to your computer and use it in GitHub Desktop.
Trying to accommodate multiple "return values" in Streamline syntax.
// here's an example third-party, out-of-my-control function.
// it calls the callback with multiple non-error values:
function addAndProfile_original(x, y, callback) {
var start = Date.now();
return setTimeout(function () {
var time = Date.now() - start;
callback(null, x + y, time);
}, 1000);
}
// i want to be able to call addAndProfile "synchronously" using Streamline,
// but still get access to its multiple "return values". e.g.:
// var result = addAndProfile(1, 2, _);
// var sum = result.sum;
// var time = result.time;
// attempt 1: a normal JS wrapper for those multiple return values:
function addAndProfile_attempt1(x, y, callback) {
addAndProfile_original(x, y, function (err, sum, time) {
callback(err, {
sum: sum,
time: time,
});
});
}
function str(result) {
return result.sum + ' (took ' + result.time + ' ms)';
}
console.log();
console.log('testing attempt 1 in sequence:'); // works!
console.log('1 + 2 = ' + str(addAndProfile_attempt1(1, 2, _)));
console.log('3 + 4 = ' + str(addAndProfile_attempt1(3, 4, _)));
console.log('testing attempt 1 in parallel:');
console.log("doesn't work; no futures injection since not in streamline syntax");
/*
var fut1 = addAndProfile_attempt1(1, 2);
var fut2 = addAndProfile_attempt1(3, 4);
console.log('1 + 2 = ' + str(fut1(_)));
console.log('3 + 4 = ' + str(fut2(_)));
*/
// attempt 2: streamline syntax to get future injection:
// doesn't compile: "Function contains async calls but does not have _ parameter"
/*
function addAndProfile_attempt2(x, y, _) {
addAndProfile_original(x, y, function (err, sum, time) {
_(err, {
sum: sum,
time: time,
});
});
}
*/
// attempt 3: it feels incorrect, but what if we add a fake _ param?
// doesn't compile still: "invalid use of '_' parameter"
/*
function addAndProfile_attempt3(x, y, _) {
addAndProfile_original(x, y, function (err, sum, time, _) {
_(err, {
sum: sum,
time: time,
});
});
}
*/
// attempt 4: extend previous to synchronous style. this compiles!
function addAndProfile_attempt4(x, y, _) {
addAndProfile_original(x, y, function (err, sum, time, _) {
if (err) throw err;
return {
sum: sum,
time: time,
};
});
}
console.log();
console.log('testing attempt 4 in sequence:');
console.log("doesn't work; original callback never passes a callback; returns a future");
/*
console.log('1 + 2 = ' + str(addAndProfile_attempt4(1, 2, _)));
console.log('3 + 4 = ' + str(addAndProfile_attempt4(3, 4, _)));
*/
console.log('testing attempt 4 in parallel:');
console.log("also doesn't work; similar reason except delayed to future invocation");
/*
var fut1 = addAndProfile_attempt4(1, 2);
var fut2 = addAndProfile_attempt4(3, 4);
console.log('1 + 2 = ' + str(fut1(_)));
console.log('3 + 4 = ' + str(fut2(_)));
*/
// final attempt: back to attempt 1 and implement futures manually.
// usage referenced from (see "Going one step further"):
// http://bjouhier.wordpress.com/2011/04/04/currying-the-callback-or-the-essence-of-futures/
var future = require('streamline').future;
function addAndProfile_attempt5(x, y, callback) {
if (!callback) return future(arguments.callee, arguments);
addAndProfile_original(x, y, function (err, sum, time) {
callback(err, {
sum: sum,
time: time,
});
});
}
console.log();
console.log('testing attempt 5 in sequence:'); // works!
console.log('1 + 2 = ' + str(addAndProfile_attempt5(1, 2, _)));
console.log('3 + 4 = ' + str(addAndProfile_attempt5(3, 4, _)));
console.log('testing attempt 5 in parallel:'); // works too!
var fut1 = addAndProfile_attempt5(1, 2);
var fut2 = addAndProfile_attempt5(3, 4);
console.log('1 + 2 = ' + str(fut1(_)));
console.log('3 + 4 = ' + str(fut2(_)));
console.log();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment