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 713 times
Edit
0
Community Evaluations
developer avatar
Auteur anonyme
28/04/2023
A peine le temps de lire la question. Si vous faites une question alambiquée, laissez du temps pour y répondre convenablement...
developer avatar
Auteur anonyme
05/06/2023
Merci pour le retour, le temps a été doublé sur cette question.