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

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 21 Next »

This API wraps an asynchronous function so that it can be called in a top-down manner within a fiber.  

The API assumes that the function accepts a callback as its last parameter in the following format:

function(error, response)

This is typical for virtually all asynchronous Node.js requests.

The API can also accept an object which contains a set of asynchronous functions, in which case it will act the same as the pjs.fiber.wrapAll() API.


  • asynchronous function
  • this argument (optional)
    (info) Typically, the parent object of the function is passed; this is required if internally the function uses the this keyword.

Alternative Parameters

  • object containing asynchronous functions

Return Value

A top-down function is returned.

Exception Handling

If the callback receives an error object, an error is thrown.


Wrap fs.readFile in a fiber
const fs = require('fs');
var readFile = pjs.fiber.wrap(fs.readFile);
var text = readFile("somefile.txt", 'utf8');
Wrap all fs API in a fiber
const fs = pjs.fiber.wrap(require('fs'));
var text = fs.fiber.readFile("somefile.txt", 'utf8');

Video Tutorial


  • No labels