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

379
Vistas
node.js: How to yield from within a C++ module?

Background: v8 supports yield (old news, I know), and that is great to do away with callbacks in javascript code such as what is used in node.js (see https://wingolog.org/archives/2013/05/08/generators-in-v8 )

The question: since a javascript co-routine can call C++ code (via a module), how can the invoked C++ perform a yield operation? To illustrate:

// javascript
function* values()
{
    yield 27;
    mycppmodule.someFunction();
}

// c++
mycppmodule::someFunction()
{
    __somehow_yield( 28 ); // how can we make this happen?
}

// user of the code above
var o = values();
o.next(); //  returns 27 - came from javascript above
o.next(); //  returns 28  - came from c++ above, which is invoked by js

I suspect the answer is somewhere in the V8 API (https://v8docs.nodesource.com/node-7.4/) , but my search did not yield (pun intended) any results...

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your mycppmodule.someFunction(); couldn't yield the value from the generator returned by the generator function values() even if it was written in JavaScript. If you want to do something in C++ that works like some other code in JavaScript then you must make sure that it would work in JavaScript in the first place.

If you want to use generator-based coroutines (like with co module or Bluebirds's coroutine) then the situation is a little bit different - any coroutine just returns a promise and what you yield from a generator is actually a promise that you want resolved and injected in the next run of the generator, but it doesn't seem to be the case here.

That having been said, first make sure that you idea can be implemented in JsvaScript and I argue that this:

function* values() {
    yield 27;
    someFunction();
}

let someFunction = // fill the blanks

var o = values();
o.next(); // returns 27 - came from generator
o.next(); // returns 28 - came from someFunction()

cannot be implemented without changing the values() generator function to something like:

function* values() {
    yield 27;
    let gen = someFunction();
    yield gen.next();
}

or:

function* values() {
    yield 27;
    yield* someFunction();
}

If you are OK with changing the original values() function then read on.

Now, all that a generator function does is it returns an object that is a generator. That generator has methods like .next(), .throw() and .return(). If you create an object in C++ that has the right interface then you might be able to use it as a generator in JavaScript with keywords like yield* but I would have to test it to make sure about it.

See the docs:

  • yield
  • yield*
  • function*
  • Generator
over 4 years ago · Santiago Trujillo 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