foreach (var player in players) {
while (true) {
var name = await Ask("What's your name");
if (IsValidName(name)) {
player.name = name;
break;
}
}
}
Assuming `Ask` is an asynchronous operation and must not block the UI thread.Note that second player is only asked after the first player has given a valid name.
(And the code structure reflects that :-)
My point is of course it's doable with callbacks, but I spent more time indenting this code than writing it, and I darn well know I'm not smart enough to spell out the correct callback-style code in a comment field on Hacker News. And if I suddenly had to add error handling...
But to answer your question, since these can't be done in parallel, you'd have to keep track of which player you're asking:
var playersAsked = 0;
var askNextPlayerHisName = function(done){
if (playersAsked === players.length) done();
Ask("What's your name?", function(name){
if (IsValidName(name)){
players[playersAsked].name = name;
playersAsked++;
}
askNextPlayerHisName(done);
});
};
askNextPlayerHisName(function(){/*...*/}); (function(players) {
var cur_idx = 0;
function cb(name, err) {
// just kidding, not going to handle errors!
if (IsValidName(name)) {
players[cur_idx].name = name;
cur_idx++;
}
if (cur_idx < players.length) {
Ask("What's your name", cb);
}
}
Ask("What's your name", cb);
})(players);
This is of course completely awful. just2n's reply is a nicer realization of the same concept. sprobertson's reply clearly will not work as written, and I don't expect it's even possible to condense this code into a single call to eachSeries. async.eachSeries(players, askName, function (err) {});
// this is someplace it should go
function askName(player, callback) {
Ask("What's your name", function (err, name) {
if (err) { return callback(err); }
if (IsValidName(name)) {
player.name = name;
callback(null);
} else {
askName(player, callback);
}
});
}
This follows Node's err convention. Note I use a bit of old fashion recursion to handle the asking for a valid name. This will not stack overflow due to the fact Ask is async. You could inline askName, but then you wouldn't have a nice little unit testable function. async.eachSeries players, ask, ->
# Well that was easy enough...
printRoster(players) function askPlayers(players, fn) {
if (!players.length) return fn();
var player = players[0];
ask("What's your name", function(name) {
if (isValidName(name)) {
player.name = name;
players.shift();
}
askPlayers(players, fn);
});
} async.eachSeries(
players,
function(player, playercb) {
var valid = false;
async.whilst(
function() { return !valid; },
function(wcb) {
Ask("What's your name", function(name) {
if( IsValidName(name) ) {
player.name = name;
valid = true;
}
wcb();
});
},
playercb);
},
function() { /* done */ }
);
More lines than the foreach loop, but on the other hand, if this was an operation you wanted to do in parallel instead of sequentially, that'd be impossible with the simple loop construct. for player in players
get_name_for = (player) ->
ask "what's your name?", (response) ->
if is_valid_name response
player.name = response
else get_name_for player
get_name_for playerEdit: I was optimistically assuming that the consumer of the many "asks" that are created all at once would process them sequentially, dealing with one and invoking the callback before dealing with the next. If you do not assume this, my problem disappears and you get the simpler problem of spawning many prompts simultaneously.
was your point that it couldn't be done with callbacks? or couldn't be done easily? or not easily alongside traditional flow control like for loops?
I agree, await and async in C# are very nice, I just took your post as a challenge.
get_player_name = (player, next) ->
ask "what's your name?", (response) ->
if is_valid_name response
player.name = response
next!
else get_player_name player, next
get_player_names = ([player,...players]) ->
get_player_name player, ->
if players.length > 0
get_player_names players
get_player_names playersIt took me about as long as I typed this code to write it.
Of course it is doable with callbacks, but I know I'm not smart enough to do it in a comment field on HN.
function getAllNames(players, callback){
function getPlayer(i, players, callback){
Ask("What's your name", function(name){
if(isValidName(name)){
players[i++].name = name;
}
if(i == players.length){
callback(players);
} else {
getPlayer(i, players, callback);
}
});
}
getPlayer(0, players, callback)
} if not song.artist
@getArtist song.id, (err, artist) =>
song.artist = artist
@save song
@addToCatalog song
#...
else
@save song
@addToCatalog song
#...
Callbacks will force you to move @save, @addCatalog, ... into a separate function. Completely messing up the logical sequence of operations. players.forEach(function() {
'use strict';
var player = this;
var name = Ask("What's your name?, function(name) {
if (isValidName(name) {
player.name = name;
}
});
});
Callbacks 'get crazy' when you've got more than one I think, and thankfully someone smart has made a library you can use to manage them!
https://github.com/caolan/async
Saying that, I don't mind the way things look with the whole await/async stuff in C# and etc. However I don't think we should be waving our arms around saying callbacks are like goto, they so completely are not! I have written heaps of stuff with callbacks and it's _not that confusing or unmaintainable_. It's just different.