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

149
Visualizações
Safe item removal by index from changing array

Lets say items are frequently added via push into an array. Another async function(which runs independently in a setInterval context) removes random items from the array.

In this second function, when in one line I evaluate the index of the item to be removed, the array could change before I reach the next line where I do array.splice(index, 1). Correct? The wrong item would be removed.

How should I solve that?

EDIT: example:

async function analyze() {

  let pendingTxs = [] 

  web3.eth.subscribe('pendingTransactions', function(error, result){
    if (error) console.log(error);
  })
  .on("data", async function(hash){   
    pendingTxs.push(hash)    
  })  

  setInterval(async () => {    
    
    let promises = pendingTxs.map(pTx => {return web3.eth.getTransaction(pTx)})

    let updatedTransactions = await Promise.all(promises)

    for (let index = 0; index < updatedTransactions.length; index++) {
      const txUpdated = updatedTransactions[index];
      if (txUpdated.transactionIndex != null) {
        const index = pendingTxs.indexOf(txUpdated.hash)
        if (index > -1) {          
          pendingTxs.splice(index, 1);          
        }
      }
    }

  },250)
  
}

analyze()

EDIT2: I think Chris may be right in this case.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

No.

const index = pendingTxs.indexOf(txUpdated.hash)
if (index > -1) {          
  pendingTxs.splice(index, 1);          
}

runs as a unit.

JavaScript's async functions are cooperative, not preemptive.

"Difference between Preemptive and Cooperative Multitasking"

In cooperative multitasking, the [controller] never initiates context switching from the running [thread] to another [thread]. A context switch occurs only when the [thread] voluntarily yields control

since JS is based on event loop concurrency

So if you do an await or yield, then other code may run, but if you do not explicitly do one of those pausing actions, then the instructions above run as a unit.

SharedArrayBuffers are an exception to this because the host is allowed to share their contents widely, but Arrays are not so shared.

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