Page tree
Skip to end of metadata
Go to start of metadata

The fiber.run() and the Fiber.yield() methods allow you to wrap or call non-standard asynchronous functions in a top-down manner to run in a fiber. It is used on asynchronous functions whose callbacks are not the last parameter or are not in the following format:

  • function(error, response)

For standard Node.js asynchronous functions, pjs.fiber.wrap() or pjs.fiber.run() will be simpler to use.


Parameters

The fiber.run() method accepts a return value as a parameter. fiber is derived from Fiber.current outside of the asynchronous callback.

The Fiber.yield() method accepts no parameters.


Return value

The Fiber.yield() method returns the value passed to the Fiber.current.run() method.


Example

const Fiber = require('profoundjs-fibers');
 
function wait(sec) {
  var fiber = Fiber.current;
  if (fiber == null) {
    // This would normally never happen
    // Unless an asynchronous process was launched without using one of the Fiber API
    // In Profound.js, you should always use Fiber API to call an asynchronous process
    throw new Error("There is no fiber running.");
  }
 
  setTimeout(function() {
    fiber.run("Successfully waited " + sec + " seconds!");
  }, sec * 1000);
  return Fiber.yield();
}
 
var returnValue = wait(5);
console.log(returnValue);  // This outputs: Successfully waited 5 seconds!