-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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.
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
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 themap(..)
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:
--or--