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

278
Vistas
why is my async code inside for loop not running sequentially?

Why do all my function calls get executed in parallel here when using await? What needs to be changed in order to start a function call once the previous is done?

    for (let collection of collections) {
      await this.updateListings(collection)
    }
    

  private async updateListings(collection: Collection) {

    try {

      const startTime = Date.now();
      const supply = collection.stats.total_supply;
      const contract_address = collection.primary_asset_contracts[0].address;
      const token_ids = [];

      for (let i = 1; i <= supply; i++) {
        token_ids.push(i);
      }

      let offset = 0;
      let limit = 30;

      while (offset + limit <= supply) {

        this.apiKey = this.apiKeys.find(key => key !== this.apiKey);    //rotate api key on each request

        const ids = token_ids.splice(0, 30);
        const params = new URLSearchParams();
        params.set("offset", offset.toString());
        params.set("limit", limit.toString());
        params.set("side", "1");
        params.set("sale_kind", "0");
        params.set("order_by", "eth_price");
        params.set("order_direction", "asc");
        params.set("asset_contract_address", contract_address);

        for (let i = 0; i < ids.length; i++) {
          params.append("token_ids", ids[i])
        }

        await this.delayRequest(700);
        const response = await axios.get(process.env.OPENSEA_API_BASE_URL + "/orders", {
          headers: {
            "X-API-KEY": this.apiKey,
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
          },
          params: params
        })
        const { orders } = response.data;

        if (orders.length > 0) await getRepository(Listing).save(orders);

        offset += limit;
        console.log(`update listings for ${collection.slug} status: ${offset} / ${supply}`)
      }

      const endTime = Date.now() - startTime;
      console.log("fetched listings for " + collection.slug + " in " + endTime + " ms")
    } catch (error) {
      console.log("updating listings for " + collection.slug + " failed", error.message);
    }

  }

  constructor() {
    setTimeout(() => {
      this.update();
    }, 6000)
  }

What I expect is that the first iteration finishes before it proceeds but its calling each iteration "updateListings(coll)" at the same time.

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

0

In fact. your code is executed sequentially. How did you check that your code was running in parallel?

You can use Array.reduce to make it sequentially:

return collections.reduce((currentPromiseChain, collection) => {
    return currentPromiseChain.then(() => this.updateListings(collection));
}, Promise.resolve());
about 4 years ago · Juan Pablo Isaza Denunciar

0

Just add an await before the condition. This will ensure each loop finishes before continuing to the next one.

Note: not fully sure on the compatibility for this, you may need check for compatibility.

for await (let collection of collections) { //notice the await in this line.
    await this.updateListings(collection)
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

This is to demostrate there isn't anything wrong with the way you are calling for loop, and also how you have a while loop inside your updateListing function.

There's probably something you didn't show?

The only reason I can see, affecting your codes, is if there are errors, and your try/catch block returns the promise with error immediately, making it appears it is running in parallel.

const whileLoopFunction = async(value) => {

    let limit = 0;
    let supply = 10

    while (limit < supply) {

       await new Promise(resolve => setTimeout(resolve,100))
       limit++
    }
    console.log('While loop complete for value ', value)

};

(async() => {

const collections = [ 1, 2, 3, 4, 5 ]

console.log("For loop works with await")
for (let collection of collections) {
    await whileLoopFunction(collection)
}




})()

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