JavaScript Teaser: Jan. 16, 2015

Written by Yuri Takhteyev

Here's the second JavaScript Teaser in our series for JavaScript ninjas. Can you figure it out? Please email Yuri if you want feedback on your answer. Do also let us know your thoughts on the question below (but don't post the answers in the comments section this time, so more people can send answers in.) Thanks!

Consider the following code:

function getTheAnswerAsync() {  
  return Q.when(42);

function getTheQuestionAsync(answer) {  
  if (answer===42) {
    return Q.when('What do you get if you multiply six by nine?');
  } else {
    return Q.when('Don\'t panic!');

process(function *() {  
  var theAnswer = yield getTheAnswerAsync();
  console.log('The answer:', theAnswer);
  var theQuestion = yield getTheQuestionAsync(theAnswer);
  console.log('The question:', theQuestion);

Write the implementation of process() that would make this work, outputting:

The answer: 42  
The question: What do you get if you multiply six by nine?  

Hint: this will only work on node ≥ 0.11 and you will need to use --harmony flag.