Skip to content

Instantly share code, notes, and snippets.

@EdJ
Created January 23, 2014 16:02
Show Gist options
  • Save EdJ/2eed46430a9d184c0fef to your computer and use it in GitHub Desktop.
Save EdJ/2eed46430a9d184c0fef to your computer and use it in GitHub Desktop.
Short example of something I'm a bit confused about in the current implementation of Asynquence's map() implementation.
var ASQ = require('asynquence');
require('asynquence-contrib');
var returnsArr = function returnsArr(c) { c && c([1]); return [1]};
ASQ().map(returnsArr(), function (v, c) { c(v + 1) }); // fine
ASQ().then(returnsArr).map(function (v, c) { c(v + 1) }); // doesn't work
@getify
Copy link

getify commented Jan 23, 2014

map(..) expects both parameters to be supplied... it doesn't take a "value message" from a previous step as one of the parameters.

So your first snippet provides the array and the callback params, works great.

Your second one is trying to have returnsArr() pass along a value message and have that be treated as the first parameter of the map(..) call, but that's not how it works, thus it's broken. :)

You could make a plugin that maps a previous step's value-message as a parameter to an API call, if you wanted, but that's kinda strange. value-messages that pass from step to step are treated as messages to the callback itself, not to the API method.

So, in the vein of your snippet above, you could do:

ASQ()
.val(function(){
   return "foo"; // inject a value-message into the stream
})
.map([1,2,3], function (v, c, msg) { c(v + msg) }); // "1foo","2foo","3foo"

--or--

ASQ()
.then(function(done){
   done("foo"); // inject a value-message into the stream
})
.map([1,2,3], function (v, c, msg) { c(v + msg) }); // "1foo","2foo","3foo"

@getify
Copy link

getify commented Jan 23, 2014

You could also do:

ASQ()
.val(function(){
   return [1,2,3]; // inject a value-message into the stream
})
.seq(function(arr){
   return ASQ()
   .map(arr, function (v, c) { c(v + 1) });
})
.val(function(newArr){
   console.log(newArr); // 2,3,4
});

And of course, if that was a common use-case for you, it'd be pretty trivial to make that usage of the API into a simple plugin. Almost any usage of the ASQ API can be rolled into a plugin.

@getify
Copy link

getify commented Jan 24, 2014

After thinking it over, I decided to update map(..) itself to handle this use-case. Was a simple addition and there was no need for a separate plugin.

Thanks for the idea! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment