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

273
Vistas
Llame a una función cada X segundo en un bucle en NodeJS

Mi objetivo: llamar a una función cada X segundo.

Estoy usando la API de Coinbase pro y obtengo información de mi billetera. Hay un límite de llamadas a la API por segundo. Así que me gustaría llamar a mi función getWalletHistory() para cada billetera cada 5 segundos.

Lo que tengo: se llama a mi función getWalletList(), luego espero 5 segundos para llamar a mi función getWalletHistory().

Lo que quiero: Llame a mi función getWalletHistory() para cada billetera cada 5 segundos.

 var wallet_list = []; getWalletList(); function getWalletList(){ authedClient.getAccounts().then(wallet_list_response => { wallet_list_response.forEach(async wallet => { console.log("WALLET LIST:" + wallet.id); wallet_list.push(wallet); }); console.log('**********END LOOP WALLET LIST'); getWalletHistory(wallet_list); }).catch(error => { // handle the error console.log('ERROR getAccounts'); //console.log(error); }); } const getWalletHistory = async function(wallet_args){ wallet_args.forEach(async wallet => { setTimeout(function(){ authedClient.getAccountHistory(wallet.id).then(order_list => { console.log('*********response'); }).catch(error => { // handle the error console.log('ERROR getAccountHistory'); //console.log(error); }); }, 5000); }); }

ACTUALIZAR Siguiendo el comentario de Erenn, uso SetInterval en lugar de SetTimeout:

 var wallet_list = []; getWalletList(); function getWalletList(){ authedClient.getAccounts().then(wallet_list_response => { wallet_list_response.forEach(async wallet => { console.log("WALLET LIST:" + wallet.id); setInterval(() => getWalletHistory(wallet), 5000); }); console.log('**********END LOOP WALLET LIST'); }).catch(error => { // handle the error console.log('ERROR getAccounts'); //console.log(error); }); } const getWalletHistory = async function(wallet){ console.log('*********getWalletHistory:' + wallet.currency); authedClient.getAccountHistory(wallet.id).then(order_list => { console.log('*********response order_list'); }).catch(error => { // handle the error console.log('ERROR getAccountHistory'); //console.log(error); }); }

Lo que tengo ahora: getAccountHistory() se llama cada 5 segundos para toda la billetera en un bucle infinito. Me gustaría llamar a getAccountHistory() cada 5 segundos para solo 1 billetera a la vez.

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

0

Podría mantener una cola de lista en la que setInterval podría operar cada 5000 ms. A continuación, el fragmento actualizado echa un vistazo a getWalletHistories y sus comentarios

 var wallet_list = []; getWalletList(); // responisble for maintaining queue (in the form of array) and setting interval // Create a new reference of list, pop the item, calls getWalletHistory // and once promise gets resolved, move the item back in the list (front of the array) function getWalletHistories(walletList) { let interval; // clear interval counter if getWalletHistories called once again if (interval) { clearInterval(interval); } const _list = [...walletList]; interval = setInterval(() => { // pop wallet item from list for get wallet item history const wallet = _list.pop(); getWalletHistory(wallet).then(() => { // push the popped wallet item back to the front of list _list.unshift(wallet); }); }, 5000); } function getWalletList() { authedClient .getAccounts() .then((wallet_list_response) => { getWalletHistories(wallet_list_response); console.log("**********END LOOP WALLET LIST"); }) .catch((error) => { // handle the error console.log("ERROR getAccounts"); //console.log(error); }); } function getWalletHistories(walletList) { let interval; // clear interval counter if getWalletHistories called once again if (interval) { clearInterval(interval); } const _list = [...walletList]; interval = setInterval(() => { // pop wallet item from list for getting wallet item history const wallet = _list.pop(); getWalletHistory(wallet).then(() => { // push the popped wallet item back to the front of list _list.unshift(wallet); }); }, 5000); } const getWalletHistory = async function (wallet) { console.log("*********getWalletHistory:" + wallet.currency); // return the wallet history promise return authedClient .getAccountHistory(wallet.id) .then((order_list) => { console.log("*********response order_list", order_list); }) .catch((error) => { // handle the error console.log("ERROR getAccountHistory"); //console.log(error); }); };
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