Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

385
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!