Question from the Node.js test

Write a NodeJS program that fetches 3 movies from an API and logs their id and title.

Medium

Consider the following 2 functionsgetMovie andrun:

const Qajax = require('qajax');// Library based on Q, allowing to make promises in HTTP

// Returns an HTTP promise on the url as parameter
function getMovie(url) {
  return Qajax.getJSON(url);
}

// Start the generator as a parameter and get a promise
function run(generator) {
  var iterator = generator();

  function go(result) {
    result.value.then(function(value) {
      go(iterator.next(value))
    });
  }

  go(iterator.next());
}

What will happen when the following code is called:

run(function*() {
  let mov1 = yield getMovie('https://api.myjson.com/bins/3hn4g');//{id:1, title:'Back to the future'}
  let mov2 = yield getMovie('https://api.myjson.com/bins/1gro0');//{id:2, title:'Matrix'}
  let mov3 = yield getMovie('https://api.myjson.com/bins/53igg');//{id:3, title:'Star Wars'}

  console.log(mov1.id, mov1.title);
  console.log(mov2.id, mov2.title);
  console.log(mov3.id, mov3.title);
});
Author: Jean-marie CléryStatus: PublishedQuestion passed 813 times
Edit
1
Community EvaluationsNo one has reviewed this question yet, be the first!