microPromise(uP) - A+ v1.1 compliant promises
- Promise()
- Promise.resolver()
- Promise.thenable()
- Promise.wrap()
- Promise.defer()
- Promise.async()
- Promise.isPending()
- Promise.isFulfilled()
- Promise.isRejected()
- Promise.hasResolved()
- Promise.valueOf()
- Promise.reason()
- Promise.then()
- Promise.spread()
- Promise.done()
- Promise.end()
- Promise.catch()
- Promise.fulfill()
- Promise.reject()
- Promise.resolve()
- Promise.progress()
- Promise.timeout()
- Promise.callback()
- Promise.join()
Promise([mixin]:Object, [resolver]:Function)
Promise constructor
- param Object [mixin] - Mixin promise into object
- param Function [resolver] - Resolver function(resolve,reject,progress,timeout)
- return Object Promise
Promise.resolver(resolver:Function)
Promise resolver
- param Function resolver - resolver function(fulfill,reject,progress,timeout)
- return Object Promise
Promise.thenable(p:Object)
Helper for identifying a promise-like objects or functions
- param Object p - Object or Function to test
- return Boolean - Returns true if thenable or else false
Promise.wrap(class:Function, [instance]:Object)
Wrap a promise around function or constructor
- param Function class - class to wrap
- param Object [instance] - optional instance
- return Function function to wrap
- throw
Example: wrap an Array
p = Promise; var r = ; // => calls Array constructor and returns fulfilled promise r; // => [1,2,3];
Promise.defer(-:Function, [args]:...)
Deferres a task and returns a pending promise fulfilled with the return value from task. The task may also return a promise itself which to wait on.
- param Function - task to defer
- param ... [args] - optional list of arguments
- return Object - returns a pending promise
Example: Make readFileSync async
fs = ; var asyncReadFile = PromisedeferfsreadFileSync'./index.js''utf-8'; asyncReadFile;
Promise.async(function:Function, [callback]:Function)
Make function asyncronous and fulfill/reject promise on execution.
- param Function function - function to make async
- param Function [callback] - optional callback to call
- return Object promise
Example: make readFile async
fs = ; var asyncReadFile = Promise; ;
Promise.isPending()
Check if promise is pending
- return Boolean - Returns true if pending or else false
Promise.isFulfilled()
Check if promise is fulfilled
- return Boolean - Returns true if pending or else false
Promise.isRejected()
Check if promise is rejeced
- return Boolean - Returns true if pending or else false
Promise.hasResolved()
Check if promise has resolved
- return Boolean - Returns true if pending or else false
Promise.valueOf()
Get value if promise has been fulfilled
- return Boolean - Returns true if pending or else false
Promise.reason()
Get reason if promise has rejected
- return Boolean - Returns true if pending or else false
Promise.then(onFulfill:Function, onReject:Function, onNotify:Function)
Attaches callback,errback,notify handlers and returns a promise
- param Function onFulfill callback
- param Function onReject errback
- param Function onNotify callback
- return Object a decendant promise
Example: catch fulfillment or rejection
var p = Promise; p; p; // => 'received: hello world!'
Example: chainable then clauses
p; p; // => v is: 142, error2: [RangeError:'to large']
Example: undefined callbacks are ignored
p; p; // => we got: 5
Promise.spread(onFulfill:Function, onReject:Function, onNotify:Function)
Like then
but spreads array into multiple arguments
- param Function onFulfill callback
- param Function onReject errback
- param Function onNotify callback
- return Object a decendant promise
Example: Multiple fulfillment values
p = Promise; p p;
Promise.done(onFulfill:Function, onReject:Function, onNotify:Function)
Terminates chain of promises, calls onerror or throws on unhandled Errors
- param Function onFulfill callback
- param Function onReject errback
- param Function onNotify callback
Example: capture error with done
p;
p; // => v is: 142, throws [RangeError:'to large']
Example: define onerror handler defined on promise
p{ console }; p; p; // => v is: 142, "Sorry: [RangeError:'to large']"
Promise.end(callback:Function)
Terminates chain, invokes a callback or throws Error on error
- param Function callback - Callback with value or Error object on error.
Promise.catch(onError:Function)
Catches errors, terminates promise chain and calls errBack handler.
- param Function onError callback
- return undefined
Example: Catch error
p = Promise; p ; p;
Promise.fulfill(value:Object)
Fulfills a promise with a value
- param Object value
- return Object promise
Example: fulfillment
p = Promise; p;
Example: multiple fulfillment values in array
p = Promise; p; presolved; // => [1,2,3]
Example: Pass through opaque arguments (experimental)
p = Promise; p; p
Promise.reject(reason:Object)
Rejects promise with a reason
- param Object reason
- return Object promise
Example:
p = Promise; p; p; // outputs => 'error: some error'
Promise.resolve(value:Object)
Resolves a promise and performs unwrapping if necessary
- param Object value - Promise or literal
- return Object promise
Example: resolve a literal
p = Promise; p; // fulfills promise to 123
Example: resolve value from pending promise
p1 = Promise; p2 = Promise; p1; p2 // => p1 fulfills to 123
Promise.progress(arguments:Object)
Notifies attached handlers
- param Object arguments
Example:
p = Promise; p; p; // optputs => 'almost done' p; // outputs => 'error: some error'
Promise.timeout(time:Number, callback:Function)
Timeout a pending promise and invoke callback function on timeout. Without a callback it throws a RangeError('exceeded timeout').
- param Number time - timeout value in ms or null to clear timeout
- param Function callback - optional timeout function callback
- throw
- return Object promise
Example: timeout & abort()
var p = Promise; p; // ... after 5 secs ... => Aborted: |RangeError: 'exceeded timeout']
Example: cancel timeout
p; p; // timeout cancelled
Promise.callback(callback:Function)
Resolves promise to a nodejs styled callback function(err,ret) and passes the callbacks return value down the chain.
- param Function callback - Callback function
- return Object promise
Example:
{ iferr console; else console;
return "nice"; }
p = Promise; p ;
p; //-> success: ok
Promise.join(promises:Array)
Joins promises and collects results into an array. If any of the promises are rejected the chain is also rejected.
- param Array promises
- return Object promise
Example: join with two promises
a = Promise; b = Promise; c = Promise; a; b; a; c; // => 'hello world !''