Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

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:

Code Block
languagejavascript
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.

Section
Column
width25%

Parameters


  • 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.
Column
width25%

Alternative Parameters


  • object containing asynchronous functions
Column
width25%

Return Value


A top-down function is returned.

Column
width25%

Exception Handling


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

Shortcut

pjs.wrap() is a shortcut for pjs.fiber.wrap().

Examples

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

Video Tutorial

Section
Column
width25%
HTML
<div align="center" style="position:relative;height:180;width:320"><iframe width="320" height="180" src="https://www.youtube.com/embed/datt2wCiROI" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></div>
Column
width25%
HTML
<div align="center" style="position:relative;height:180;width:320"><iframe width="320" height="180" src="https://www.youtube.com/embed/BqoLkuQ6zvQ" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></div>
Column
width50%