Skip to content

Instantly share code, notes, and snippets.

@JamesMGreene
Created November 24, 2012 15:47
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 JamesMGreene/4140215 to your computer and use it in GitHub Desktop.
Save JamesMGreene/4140215 to your computer and use it in GitHub Desktop.
Using Q, is this the best way to pass the results of two sequenced promises to the subsequent `then`/`spread` clause?
'use strict';
var Q = require('q');
var cmd = new require('commander').Command();
var client = new require('myAwesomeApi').Client();
var getUsername = function(done) {
var username;
cmd.prompt('Username: ', function(name) {
if (!name) {
done(new Error('You must provide a username!'));
}
else {
done(null, username);
}
});
};
var getPassword = function(done) {
cmd.password('Password: ', '*', function(pass) {
if (!pass) {
done(new Error('You must provide a password!'));
}
else {
process.stdin.destroy();
done(null, pass);
}
});
};
Q.napply(getUsername, null, []).then(function(username) {
return Q.napply(getPassword, null, []).then(function(password) {
return Q.resolve([username, password]);
});
}).spread(function(username, password) {
return Q.napply(client.login, client, [username, password]);
})
@JamesMGreene
Copy link
Author

Cool, I'll go with catch (definitely prefer its obviously semantic name over fail) and an empty done. Thanks again!

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