Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

531
Vistas
Javascript Books challenge

I tried to complete a javascript assessment for a job interview but failed

My plan was to process the user object inside findPotentialLikes function but it'll always throw undefined.

It is made using Mocha and Chai testing framework and promises.

Basically my question is: how do i get the user in the findPotentialLikes promise ?

class SocialNetworkQueries {

    constructor({ fetchCurrentUser }) {
        this.fetchCurrentUser = fetchCurrentUser;
    }

    findPotentialLikes({ minimalScore } = { }) {
        
     console.log(this.fetchCurentUser);
     // I tried here to get the user in order to process the findPotentialLikes function with no luck

        return Promise.resolve();
        
    }

}


describe('SocialNetworkQueries', () => {
  describe("example from README", () => {

    it("should find potential likes", async () => {
      // given
      const user = {
        id: "mrouk3",
        likes: {
          books: ["Moby Dick", "Ulysses"],
        },
        friends: [{
          id: "YazL",
          likes: {
            books: ["Ulysses", "War and Peace"],
          }
        }],
      };

      // when
      const potentialLikes = await new SocialNetworkQueries({
        fetchCurrentUser: () => Promise.resolve(user),
      }).findPotentialLikes({ minimalScore: 0.3 });

      // then
      expect(potentialLikes).toEqual({
        books: [
          "The Great Gatsby",
          "Don Quixote",
          "War and Peace",
        ],
      });
    });

  });
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Given that the fetchCurrentUser argument is provided by the test as a function which returns a promise which resolves to a user value, you would need to retrieve the resolved value from the promise, either via async/await:

class SocialNetworkQueries {
    constructor({ fetchCurrentUser }) {
        this.fetchCurrentUser = fetchCurrentUser;
    }

    async findPotentialLikes({ minimalScore } = { }) {
        const user = await this.fetchCurrentUser();

        // ...

        return {
            books: [
                "The Great Gatsby",
                "Don Quixote",
                "War and Peace",
            ]
        };
    }
}

or logic in a .then() callback which will execute after the user has resolved:

class SocialNetworkQueries {
    constructor({ fetchCurrentUser }) {
        this.fetchCurrentUser = fetchCurrentUser;
    }

    findPotentialLikes({ minimalScore } = { }) {
        return this.fetchCurrentUser().then(
            user => {
                // ...

                return {
                    books: [
                        "The Great Gatsby",
                        "Don Quixote",
                        "War and Peace",
                    ]
                };
            }
        );
    }
}

Either approach is functionally equivalent and results in findPotentialLikes() returning a Promise which resolves with the asserted value from the test.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda