Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Trying to run multiple await methods in node JS sequentially, they are running in parallel

I'm trying to combine layers of images into one image. It works fine when I have just one method to fetch the image then another and layers them together, The problem is I need to tint the image before I draw it and they have to be drawn in a certain order. When I introduced the Sharp library to tint images into a buffer and then load the buffer and draw things start to draw out of order

here is the code

const drawLayer = async (_layer, _edition) => {
  let element = _layer.elements[Math.floor(Math.random() * _layer.elements.length)];
  addAttributes(element, _layer);

  var path = `${_layer.location}${element.fileName}`;

  console.log("start " + path);
  const buffer = await sharp(`${_layer.location}${element.fileName}`).png().toBuffer();
  console.log("sharp done " + path);
  const image = await loadImage(buffer);
  console.log("load image done " + path);

  ctx.drawImage(
    image,
    _layer.position.x,
    _layer.position.y,
    _layer.size.width,
    _layer.size.height
  );
  console.log("saving edition " + _edition);
  saveLayer(canvas, _edition);
  console.log("\n\n");
};

function draw() {
  for (let i = 1; i <= edition; i++) {
    layers.forEach((layer) => {
      drawLayer(layer, i);
    });
    addMetadata(i);
    console.log("Creating edition " + i);
  }
}

If there is only one await call for example I just load the image this way

const image = await loadImage(path);

there is no problem

a second solution I can figure out is just use the sharp to load the image in a buffer but some how i need to turn that buffer back into either an image or canvas for this method

ctx.drawImage(
    image,
    _layer.position.x,
    _layer.position.y,
    _layer.size.width,
    _layer.size.height
  );
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I believe the issue is that drawLayer is a async function, which means you need to await the function before drawing the next layer. Howwever, according to my knowledge, Array.forEach does not support async functions, so you have to do this instead.

async function draw() {
    for (let i = 1; i <= edition; i++) {
        for (let layer of layers) {
             await drawLayer(layer, i);
        });
        addMetadata(i);
        console.log("Creating edition " + i);
    }
}

I think this might do the trick.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda