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

325
Visualizações
protobufjs load() to execute synchronously in browser

I have proto file that contains all the message definitions. I am currently working on a reactjs project in which I want to dynamically use some of these proto models inside js code, and for this I am using the protobufjs library.

I have a function within which I invoke protobuf.load() to create a new object of type defined in the proto file. The code for the function looks something like this:

function doSomething () {
  // This is what I want to return
  let result = [];

  // Load the proto models and extract necessary info
  protobuf.load(protoFile, function(err, root) {
      if (err)
          throw err;
      const someMessageType = root.lookupType("someMessageType");

      // res is actually populated here
      res = Object.keys(someMessageType.fields);
  });

  return res;
}

On calling doSomething(), the result is always an empty array returned as the load function is executed asynchronously (as I understand).

Is there a clean way to make the load execute to completion before the result is returned? It is okay if the UI freezes and it is not a concern.

The protobufjs documentation specifies another syntax

protobuf.load("awesome.proto")
.then(function(root) {
   ...
});

On trying this, I get: Error: illegal token '<' (models.proto, line 1)

I am a novice when it comes to working with js. Any help on this issue would be of great help. Thanks in advance!

about 4 years ago · Juan Pablo Isaza
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