4 ms·
Coroutines (or generators) are a really nice sugar for callbacks. This looks a lot like ES6's yield, just s/await/yield/. But to answer your question, since th
by just2n 13y ago
Coroutines (or generators) are a really nice sugar for callbacks. This looks a lot like ES6's yield, just s/await/yield/.
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(){/*...*/});