README
Read Me
Introduction
This package is a "Deferred" asynchronization mechanism implementation (a part of XRT library). It also contains "Deferred Data Structure".
Installation
To install this package, type following command in your terminal:
npm install xrtlibrary-deferred --save
And then, you can import this package in your NodeJS environment with following "require" statement.
var XRTLibDeferred = require("xrtlibrary-deferred");
API (Usage)
- Deferred:
Method: +---------------------+--------------------------------------------------------------------+ | Method | Description | +---------------------+--------------------------------------------------------------------+ | constructor | (No parameter required.) | +---------------------+--------------------------------------------------------------------+ | addCallback([fn]) | Register a callback function. | +---------------------+--------------------------------------------------------------------+ | callback([...args]) | Call all registered callback functions (with the same parameters). | +---------------------+--------------------------------------------------------------------+
Example:
var deferred = new XRTLibDeferred.Deferred() deferred.addCallback(function(value) { console.log("Callback 1: " + value.toString()); }); deferred.addCallback(function(value) { console.log("Callback 2: " + value.toString()); }); deferred.callback(12345);
- DeferredQueue:
Method: +-------------+---------------------------------------+ | Method | Description | +-------------+---------------------------------------+ | constructor | (No parameter required.) | +-------------+---------------------------------------+ | put([item]) | Put an item to the tail of the queue. | +-------------+---------------------------------------+ | get() | Get an item off from the head of the | | | queue (wrapped with "Deferred"). | +-------------+---------------------------------------+ | getLength() | Get the count of items in the queue. | +-------------+---------------------------------------+ | clear() | Clear the items in the queue. | +-------------+---------------------------------------+
Example:
var queue = new XRTLibDeferred.DeferredQueue() for (var i = 0; i < 5; ++i) { queue.get().addCallback(console.log); } for (var i = 0; i < 10; ++i) { queue.put(i); } for (var i = 0; i < 5; ++i) { queue.get().addCallback(console.log); }