Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Función en JS que devuelve datos definidos con 1 segundo de retraso

Entonces, tengo una tarea, hacer una función ' mocker ' que devolverá datos definidos con 1 segundo de retraso. El problema es que soy nuevo en el mundo de JavaScript y no sé cómo hacer esto. Intenté tomar este camino:

 mocker = function mocker(data) { var delayInMilliseconds = 1000; setTimeout(function () { return data; }, delayInMilliseconds);};

Pero no satisface la tarea. Tengo este ejemplo:

 const getUsers = mocker([{id: 1, name: 'User1'}]); getUsers().then((users) => { // Will fire after 1 second. console.log(users); // result: [{id: 1, name: 'User1'}]; });

Y esta es la descripción de la función para la prueba:

 describe('mocker', () => { describe('users mocker', () => { const usersData = [{id: 1, login: 'mickey'}, {id: 2, login: 'billy77'}, {id: 3, login: 'coooool123'}]; let getUsers; beforeEach(() => { getUsers = mocker(usersData); }); it('should return users data', async () => { const resultData = await getUsers(); assert.deepStrictEqual(resultData, usersData); }); it('should return users data in asynchronous way', () => { const resultData = getUsers(); assert.notDeepStrictEqual(resultData, usersData); }); });

});

 @param data: {Array | Object} @returns {Function}

¿Pueden por favor ayudarme? Gracias por adelantado.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La prueba sugiere que es un poco más complicado que simplemente crear una función que devuelva algunos datos después de un segundo.

Esta línea:

 getUsers = mocker(usersData);

dice que mocker necesita 1) aceptar algunos datos y 2) devolver una función que asignamos a getUsers . Cuando llamamos a getUsers , llamará a esa función para devolver la promesa de datos.

Así que cuando lleguemos aquí:

 const resultData = await getUsers();

estamos llamando a esa función que asignamos a getUsers que promete devolver algunos datos (o no) después de un segundo.

 const usersData = [{id: 1, login: 'mickey'}, {id: 2, login: 'billy77'}, {id: 3, login: 'coooool123'}]; // `mocker` accepts some data and returns a // function. This function is assigned to `getData`, and // when `getData` is called this is the function that gets executed. // It returns a promise that after one second data will be returned. function mocker(usersData) { return function () { return new Promise((res, rej) => { setTimeout(() => res(usersData), 1000); }); } } // So we call `mocker` and assign the function it // returns to `getUsers` const getUsers = mocker(usersData); async function main() { // We can then call `getUsers` to get the data // after one second has passed console.log(await getUsers()); } main();

Documentación adicional

  • promesas

  • async / await

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!