• Stars
    star
    237
  • Rank 169,885 (Top 4 %)
  • Language
    JavaScript
  • Created over 14 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Promise-based IO for JavaScript.

Promised-IO is a cross-platform package for asynchronous promise-based IO. Promises provide a simple robust mechanism for asynchronicity with separation of concerns by encapsulating eventual completion of an operation with side effect free callback registration separate from call invocation. Promised-IO provides cross-platform file, HTTP, and system interaction with promises for asynchronous operations.

Promised-IO also utilizes "lazy arrays" for progressively completed actions or for streaming of data. Lazy arrays provide all the standard iterative Array methods for receiving callbacks as actions are completed. Lazy arrays are utilized for progressive loading of files and HTTP responses.

Installation

Promised-IO can be installed via npm:

npm install promised-io

promise

The promise module provides the primary tools for creating new promises and interacting with promises. The promise API used by promised-io is the Promises/A proposal used by Dojo, jQuery, and other toolkits. Within promised-io, a promise is defined as any object that implements the Promises/A API, that is they provide a then() method that can take a callback. The then() methods definition is:

promise.then(fulfilledHandler, errorHandler);

Promises can originate from a variety of sources, and promised-io provides a constructor, Deferred, to create promises.

when

when = require("promised-io/promise").when;
when(promiseOrValue, fulfilledHandler, errorHandler);

You can pass a promise to the when() function and the fulfillment and error handlers will be registered for it's completion or you can pass a regular value, and the fulfillment handler will be immediately be called. The when function is a staple of working with promises because it allows you to write code that normalizes interaction with synchronous values and asynchronous promises. If you pass in a promise, a new promise for the result of execution of the callback handler will be returned. If you pass a normal value, the return value will be the value returned from the fulfilledHandler.

Deferred

deferred = require("promised-io/promise").Deferred(canceler);

The Deferred constructor is the primary mechanism for creating new promises. The Deferred object is a form of a promise with an interface for fulfilling or rejecting the promise. A Deferred object is a means for a producer to resolve a promise and it also provides a promise for consumers that are listening for the resolution of the promise. The basic usage pattern looks like:

var Deferred = require("promised-io/promise").Deferred;
function delay(ms, value){
	// create a new Deferred
	var deferred = new Deferred();
	setTimeout(function(){
		// fulfill the deferred/promise, all listeners to the promise will be notified, and
		// provided the value as the value of the promise
		deferred.resolve(value);
	}, ms);
	// return the promise that is associated with the Deferred object
	return deferred.promise;
}

The Deferred can optionally take a canceler function. This function will cause resulting promises to have a cancel() method, and if the cancel() method is called, the Deferred will be canceled and the canceler function will be called.

The Deferred object has the following methods and properties:

resolve

deferred.resolve(value);

This will fulfill the Deferred's promise with the provided value. The fulfillment listeners to the promise will be notified.

reject

deferred.reject(error);

This will reject the Deferred's promise with the provided error. The error listeners to the promise will be notified.

promise

This is the promise object associated with the Deferred instance. The promise object will not have any of the Deferred's fulfill or reject methods, and only provides an interface for listening. This can be safely provided to consumers without any chance of being modified.

cancel

deferred.cancel();

This will cancel the Deferred.

currentContext

One of the challenges with working asynchronous code is that there can be times when you wish for some contextual state information to be preserved across multiple asynchronous actions, without having to actually pass the state to each function in the asynchronous chain. Common examples of such contextual state would be tracking the current transaction or the currently logged in user. Such state information could be stored in a singleton (a module property or a global variable), but with asynchronous actions being interleaved, this is unsuitable for tracking state across asynchronous continuations of an action.

The promised-io package's promise module provides a facility for tracking state across asynchronous operations. The promise module tracks the "currentContext" global variable, and whatever value that was in the variable at the time a promise was created will be restored when that promise is fulfilled (or rejected).

all

group = require("promised-io/promise").all(arrayOfPromises);

The all() function can be passed an array of promises, or multiple promises as individual arguments, and all() will return a new promise that represents the completed values when all the promises have been fulfilled. This allows you to easily run multiple asynchronous actions, and wait for the completion ("join") of all the actions. For example:

group = all(promise1, promise2, promise3);
group.then(function(array){
	var value1 = array[0]; // result of promise1
	var value2 = array[1]; // result of promise2
	var value3 = array[2]; // result of promise3
});

first

first = require("promised-io/promise").first(arrayOfPromises);

The first() function can be passed an array of promises, or multiple promises as individual arguments, and first() will return a new promise that represents the completed value when the first promise is fulfilled. This allows you to run multiple asynchronous actions and get the first result. For example:

response = first(requestToMainSite, requestToMirrorSite1, requestToMirrorSite2);
response.then(function(response){
	// response from the first site to respond
});

seq

result = require("promised-io/promise").seq(arrayOfActionFunctions, startingValue);

The seq() function can be passed an array of functions, and seq() will execute each function in sequence, waiting for the promise returned from each one to complete before executing the next function. Each function will be called with the result of the last function (or the startingValue for the first function).

whenPromise

resultPromise = require("promised-io/promise").whenPromise(valueOrPromise, fulfillmentHandler, errorHandler);

The whenPromise() function behaves exactly like when() except that whenPromise will always return a promise, even if a non-promise value is passed in.

allKeys

group = require("promised-io/promise").allKeys(hashOfPromises);

Takes a hash of promises and returns a promise that is fulfilled once all the promises in the hash keys are fulfilled.

fs

This module provides promise-based access to the filesystem. The API of the fs module basically follows the Node File System module API. Each of the asynchronous functions in the Node's FS API is reflected with a corresponding function in the fs module that returns a promise (instead of requiring a callback argument in the initial call). For example, where Node has fs.rename(path1, path2, [callback]), with promised-io you would call it:

var fs = require("promised-io/fs");
fs.rename(path1, path2).then(function(){
	// finished renaming
});

Any callback arguments will be the same minus the error argument:

var fs = require("promised-io/fs");
fs.readdir(path).then(function(files){
	// use the result
}, function(error) {
	// Handle errors here instead
});

One function that does differ from NodeJS's fs module is the open() function.

open

var file = require("promised-io/fs").open(path, mode);

The open() function differs from simply being a promise-based version of the Node's open() function in that it immediately returns (even though the opening of the file is asynchronous) a file object that be used to read from and write to the file.

To write to the file object, we can write:

promiseForCompletion = file.write(contents, options, encoding);

To close the file object, we can write:

promiseForCompletion = file.close();

We can also use file.writeSync and file.closeSync for the synchronous versions of these functions.

The file object is also a lazy array, which means you can read from the file using standard array methods. To asynchronously read the contents of a file, you can do:

file.forEach(function(chunk){
	// called for each chunk of the file until the end of the file is reached.
});

lazy-array

The lazy-array module provides the functionality for creating and using lazy arrays, which are objects that implement the interface of the standard iterative array methods for accessing streams of data. Array methods can be called and they will be asynchronously executed as data is available. Lazy arrays are powerful way to model asynchronous streams since they can used like other JavaScript arrays.

Typically you don't need to directly use this module, rather other IO modules like the file system (fs) and HTTP (http-client) modules provide lazy arrays that you can interact with. For example, we could search through a file for the string "lazy" and stop reading once we find it using the standard some() method:

if(file.some(function(chunk){
	return chunk.toString().indexOf("lazy") > -1;
}));

Lazy arrays include the follow standard array methods, providing access to the data as the stream data becomes available:

  • forEach
  • concat

Additional iterative methods can also access data as it available and as it is requested from the returned lazy array. This means that in order for this function to be excuted, the resulting array should have a forEach (or any of the other standard methods) called on it to trigger the request for data. These methods follow this behavior:

  • filter
  • every
  • some
  • map

And also these standard methods, although these must fully fetch the stream:

  • join
  • sort
  • reverse

Also the following additional methods are available on lazy arrays:

  • toRealArray() - This will fetch all the data and return it as a real JavaScript array.
  • get(index) - This retrieves an element by index.

LazyArray

lazyArray = require("promised-io/lazy-array").LazyArray({
	some: someImplementation,
	length: arrayLength
});

This function is a constructor for creating your own lazy arrays. With this function, you don't need to implement the entire set of array methods, you can just implement the some() method and provide an array length, if it is known.

first

first = require("promised-io/lazy-array").first(lazyArray);

This function returns the first element in a lazy array.

last

last = require("promised-io/lazy-array").last(lazyArray);

This function returns the last element in a lazy array.

get

item = require("promised-io/lazy-array").get(index);

This function returns an element by index from a lazy array.

http-client

This module provides convenient promise-based access to making HTTP requests.

Promised-IO is part of the Persevere project, and therefore is licensed under the AFL or BSD license. The Persevere project is administered under the Dojo foundation, and all contributions require a Dojo CLA.

More Repositories

1

json-schema

JSON Schema specifications, reference schemas, and a CommonJS implementation
JavaScript
505
star
2

multi-node

Multi-node provides launching of multiple NodeJS processes for TCP/HTTP serving
JavaScript
491
star
3

node-promise

Promise utilities for Node
JavaScript
399
star
4

lmdb-js

Simple, efficient, ultra-fast, scalable data store wrapper for LMDB
JavaScript
358
star
5

msgpackr

Ultra-fast MessagePack implementation with extension for record and structural cloning / msgpack.org[JavaScript/NodeJS]
JavaScript
339
star
6

put-selector

A high-performance, lightweight function for creating and manipulating DOM elements with succinct, elegant, familiar CSS selector-based syntax
JavaScript
290
star
7

nodules

Asynchronous URL-based CommonJS module loader for Node with automated dependency resolution and module reloading
JavaScript
198
star
8

cbor-x

Ultra-fast CBOR encoder/decoder with extensions for records and structural cloning
JavaScript
176
star
9

alkali

Alkali is library for functional reactive data flows that drive native-based UI elements
JavaScript
162
star
10

compose

ComposeJS is a lightweight JavaScript library for object composition
JavaScript
127
star
11

xstyle

A declarative, reactive framework that extends CSS
JavaScript
98
star
12

cpm

CommonJS Package Manager
JavaScript
95
star
13

commonjs-utils

Utility modules for CommonJS
JavaScript
89
star
14

transporter

Transporter is JSGI application that serves modules to the browser with dependencies included and packaged in the CommonJS module transport format.
JavaScript
40
star
15

dbind

Data bindings for Dojo
JavaScript
29
star
16

weak-lru-cache

A cache using LRU and weak references to cache data in a way that works in harmony with garbage collection
JavaScript
28
star
17

persevere

The Persevere server features a secure RESTful JSON interface for data interaction and storage of dynamic data, JSONQuery/JSONPath querying, Comet-based real-time data notification through Rest Channels and Bayeux support, class-based based object-oriented business logic with data integrity and validation through JSON Schema in its server-side JavaScript environment.
JavaScript
21
star
18

patr

Promise-based asynchronous test runner for JavaScript
JavaScript
16
star
19

lmdbx-js

Wrapper for libmdbx
C++
15
star
20

ua-optimized

AMD plugin for UA-based optimization of feature-detection-has.js based modules
JavaScript
13
star
21

alkali-todo

TodoMVC written with Alkali
JavaScript
12
star
22

each

Multi-purpose iteration function
JavaScript
9
star
23

eslint-plugin-auto-import

ESLint plugin for automatically importing modules
JavaScript
9
star
24

commonjs-package-template

A template for packages in the Dojo foundation package repository
JavaScript
8
star
25

cpr

CommonJS Package Repository with Git integration
JavaScript
7
star
26

bindr

Bindr is a declarative functional reactive binding language
JavaScript
7
star
27

cobase

Composable data stores from reactive JavaScript Join, Transform, Index, and Reduce functions, built on Alkali & LevelDB
TypeScript
7
star
28

babel-plugin-transform-safely

A Babel plugin for transforming expressions with safe property access and modifications, checking parent existence
JavaScript
7
star
29

litmus

An viewer/editor for exploring Alkali variable data flow/dependencies, and their connection to visual DOM elements on a page
JavaScript
7
star
30

cbor-records

Registration for CBOR tag for records
6
star
31

json-tools

CommonJS tools for interaction with JSON and JSON-style structured data
JavaScript
5
star
32

ordered-binary

Representation of JavaScript primitives as a Buffer such that binary order matches natural order of primitives
JavaScript
5
star
33

caesium

Caesium is a collection of CSS tools, including an AMD CSS loader and builder.
JavaScript
4
star
34

node-commonjs

Modules implementing CommonJS specifications on top of Node
JavaScript
4
star
35

babel-plugin-transform-alkali

Babel plugin to transform reactive expressions using alkali.
JavaScript
3
star
36

splinterdb-js

Initial exploration of JS library for SplinterDB
JavaScript
3
star
37

msgpackr-extract

Node addon for string extraction for msgpackr
C++
3
star
38

jss

JSON Stylesheets
JavaScript
2
star
39

coffeetoes6

A very simple coffeescript to ES6 converter with minimal code modification without extra syntactic requirements (you will need to make fixes afterwards)
JavaScript
2
star
40

window-name

Cross-domain browser communication using the window.name transport
JavaScript
2
star
41

literally-typed

A literate form of TypeScript for embedding type information in documentation
JavaScript
2
star
42

xstyle-todo

TodoMVC on xstyle (and dstore)
JavaScript
2
star
43

cbor-extract

C++
2
star
44

persvr-org

Code/contents of persvr.org
JavaScript
1
star
45

backtrace-c

Print C/C++ level backtraces in NodeJS
1
star
46

mysql-store

A MySQL object store implementation for NodeJS
1
star
47

ddoc

dgrid based documentation
JavaScript
1
star
48

db-benchmark

JavaScript
1
star
49

presly

Why am I using Keynote and Powerpoint when I could use dojo.gfx?
JavaScript
1
star