question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Is there a better way to do things in a series?

See original GitHub issue

I have the following code:

// Use Bluebird to promisify the async library.
var Promise = require('bluebird');
var async = Promise.promisifyAll(require('async'));

// Create readline interface to ask for user input.
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

// Concurrently grab the contents of a template file while simultaneously prompting the user
// for details that will be inserted into the compiled .gitconfig file.
Promise.join(
  // Use promisified async library to serially obtain user input for username and email while
  // still avoiding nested callbacks and keeping things readable.
  async.seriesAsync({
    email: function (done) {
      rl.question('What is your Git email? ', done.bind(null, null));
    },
    user: function (done) {
      rl.question('What is your Git username? ', done.bind(null, null));
    }
  }),
  fs.readFileAsync(path.join(__dirname, '.gitconfig.template')).then(function (templateBuf) {
    return templateBuf.toString();
  }),
  function (gitDetails, template) {
    console.log(gitDetails.email);
    console.log(gitDetails.user);
    console.log(template);
    /* build compiled template using user input */
  }
);

Note: done.bind(null, null) was just a shortcut for function (answer) { done(null, answer); } in case that was confusing at all.

It feels weird to use Bluebird and Async in conjunction like that, but then again the two libraries aren’t really doing the same things. Is there a simple readable way to do what I’m doing with Async but only using Bluebird? The first prompt must be complete before the second one begins. I tried making promisifed versions of the question method but the call to questionAsync would run for both questions, essentially starting two stdin prompts at the same time. When this happens only the first prompt actually works. I need to ensure that the first prompt got an answer before invoking the second prompt at all.

I couldn’t think of a better way to do it without nesting the second call to rl.question within the callback for the first question.

  new Promise(function (resolve, reject) {
    rl.question('What is your Git email? ', function (gitEmail) {
      rl.question('What is your Git username? ', function (gitUser) {
        resolve({
          email: gitEmail,
          user: gitUser
        });
      });
    });
  })

That’s not sustainable though if I continue to add questions later on. So what do you do in the case where the logic that returns the promise in the first place shouldn’t be run until the prior promise completes?

Sorry if this question is naive. I’m somewhat new to Bluebird and promises.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
petkaantonovcommented, Dec 16, 2014

There is a promise way to do everything async can do so it’s pretty unnecessary and complicated to use both.

var Promise = require("./js/main/bluebird.js");
var readline = require("readline");

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

var question = Promise.promisify(function(question, callback) {
    rl.question(question, callback.bind(null, null));
});


var email = question('What is your Git email?');
var user = email.then(function() {
    return question('What is your Git username?');
});
var template = fs.readFileAsync(path.join(__dirname, '.gitconfig.template')).call("toString");

Promise.join(email, user, template, function(email, user, template) {
    console.log(email, user, template);
});
0reactions
chevtekcommented, Dec 16, 2014

Now that’s nifty. I will definitely be using this. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Item in a Series | Grammar Bytes!
Recognize an item in a series when you find one. Items in a series occur whenever a sentence lists two or more things....
Read more >
Can you explain the sentence pattern “what better way to do ...
'what better way to do' is not a question its an opinion. Its a rhetorical question. 'what is the better way to' is...
Read more >
Commas: Items in a Series - GrammarFlip
Commas should be used when three or more items in a series are listed. A comma should be placed between each of the...
Read more >
Writing: Items in a series — Example (video) - Khan Academy
Watch Sal work through a harder items in a series question from the SAT Writing and Language Test.​
Read more >
serial sentences
Items in a series occur whenever a sentence includes a list of two or more things. The items can be any type of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found