The promise Reference Manual

This is the promise Reference Manual, version 1.0.0, generated automatically by Declt version 4.0 beta 2 "William Riker" on Sun Dec 15 07:21:01 2024 GMT+0.

Table of Contents


1 Introduction


2 Systems

The main system appears first, followed by any subsystem dependency.


2.1 promise

A small, independent promise library for asynchronous frameworks

Maintainer

Yukari Hafner <>

Author

Yukari Hafner <>

Home Page

https://shinmera.github.io/promise

Source Control

(GIT https://github.com/Shinmera/promise.git)

Bug Tracker

https://github.com/Shinmera/promise/issues

License

zlib

Version

1.0.0

Dependency

documentation-utils (system).

Source

promise.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


3.1 Lisp


3.1.1 promise/promise.asd

Source

promise.asd.

Parent Component

promise (system).

ASDF Systems

promise.


3.1.2 promise/package.lisp

Source

promise.asd.

Parent Component

promise (system).

Packages

org.shirakumo.promise.


3.1.3 promise/promise.lisp

Dependency

package.lisp (file).

Source

promise.asd.

Parent Component

promise (system).

Public Interface
Internals

3.1.4 promise/documentation.lisp

Dependency

promise.lisp (file).

Source

promise.asd.

Parent Component

promise (system).


4 Packages

Packages are listed by definition order.


4.1 org.shirakumo.promise

Source

package.lisp.

Use List

common-lisp.

Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


5.1 Public Interface


5.1.1 Macros

Macro: -> (promise &body promises)

Shorthand macro to chain promise construction functions together.

Example:

(-> (make)
(after :success ...)
(then (v) ...)
(handle (e) ...)
(finally ...))

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.

Macro: do-promised ((element sequence) &body body)

Shorthand macro for EACH.

See EACH

Package

org.shirakumo.promise.

Source

promise.lisp.

Macro: do-times-promised ((i limit) &body body)

Same as DOTIMES, but with a promise for each iteration.

See ITERATE

Package

org.shirakumo.promise.

Source

promise.lisp.

Macro: with-handlers (promise &body handlers)

Convenience macro to handle different failures.

Each form in the body should be of the structure:

(TYPE ARGLIST . BODY)

Similar to HANDLER-BIND. Each of the body forms is turned into a promise that is attached to the primary promise.

The primary promise is returned.

See PROMISE
See HANDLE

Package

org.shirakumo.promise.

Source

promise.lisp.

Macro: with-promise ((succeed &optional fail &key lifetime) &body body)

Shorthand for MAKE with an initialisation function.

SUCCEED and FAIL are variable names bound to the respective functions to manage the promise’s state. The names are both bound as a variable and bound as a function for convenience.

See MAKE

Package

org.shirakumo.promise.

Source

promise.lisp.


5.1.2 Ordinary functions

Function: after (promise &key success failure timeout lifetime)

Perform an action after the promise has reached a particular state.

Returns a new promise that encapsulates whether the attached handlers have successfully run or not. If one of the handlers is called and successfully returns, the new promise proceeds*. If a handler signals an error, the new promise fails.

If the promise is already done, the respective handler function is /still/ called asynchronously!

*When the new promise proceeds with a value from the handler, it acts differently depending on the returned value. If the value is another PROMISE, the new promise is linked to the returned promise, updating its state in accordance with the returned promise. If the value is any other type, the new promise is instead simply moved into its SUCCESS state.

The passed promise is coerced via ENSURE-PROMISE.

See ENSURE-PROMISE
See PROMISE
See THEN
See HANDLE
See FINALLY
See ALL
See ANY

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: all (promises &key lifetime)

Returns a promise that succeeds if all PROMISES succeed.

The passed promises are coerced via ENSURE-PROMISE.

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: any (promises &key lifetime)

Returns a promise that succeeds if at least one PROMISE succeeds.

The passed promises are coerced via ENSURE-PROMISE.

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: clear ()

Clear all known promises out of the system.

You should only use this if you have promises that are "stuck" and you don’t care if they are resolved or not.

See TICK-ALL

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: done-p (promise)

Returns whether the promise is "done".

A promise is considered done if it is not in the pending state, meaning if it is in either success, failure, or timeout satte.

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: each (sequence function)

Iterate over a sequence, calling FUNCTION for each element.

If FUNCTION returns a promise, iteration is suspended until the promise succeeds. Should FUNCTION ever error, or a promise it returns fail, the iteration is cancelled.

Returns a promise that encapsulates whether the sequence has successfully been iterated through.

See ITERATE
See DO-PROMISED

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: fail (promise &optional value)

Moves the promise to the failed state if possible, using the given error.

If the promise is timed out, nothing happens. If the promise is already failed or succeeded, an error is signalled.

Returns the promise.

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: finally (promise on-done)

Shorthand for attaching a handler that’s called when the promise is done.

The passed function should take no arguments.

See HANDLE

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: handle (promise on-failure &optional type)

Shorthand for attaching a failure handler.

If TYPE is passed, only invokes ON-FAILURE if the value is of TYPE.

See HANDLE

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: iterate (end-p cur-fun step-fun start function)

Create a promise that can iterate using the provided iteration functions.

This works as follows: START is used as the starting state of the iteration. Then, until END-P on the current state returns T, promises are generated. Each time calling CUR-FUN with the state to retrieve the iteration element. And STEP-FUN with the state to retrieve the successor state. FUNCTION is then called with the iteration
element. If FUNCTION returns a promise, the next iteration is chained to the completion of that promise. If not, the next iteration is chained immediately.

The promise returned by this function will only succeed once the iteration has completed.

See PROMISE
See EACH

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: make (&optional constructor &key lifetime)

Creates a new promise.

The CONSTRUCTOR, if passed, should be a function of two arguments. The two arguments are a succeed and fail function of one argument respectively, which you should call once the promise should be succeeded or failed, passing along the respective value if any.

Note that you can also force the returned promise into a state outside of using the two functions passed into the constructor, by using the manual state transition functions SUCCEED, FAIL, TIMEOUT.

The LIFETIME should be a positive REAL that determines the maximum number of seconds the promise is considered "live" for. If the promise is TICKed after LIFETIME seconds have passed, it will be moved to the TIMEOUT state. Lifetimes are useful to avoid a computation getting stuck on a promise that might never be fulfilled due to reliability issues in the system.

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: pend (&key lifetime success failure)

Create a dummy promise.

This is the same as using MAKE. If SUCCESS or FAILURE are passed respectively, the success/failure function is called immediately with the given value from the argument.

See MAKE

Package

org.shirakumo.promise.

Source

promise.lisp.

Reader: state (instance)

Returns the current state of the promise.

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.

Target Slot

state.

Writer: (setf state) (instance)
Package

org.shirakumo.promise.

Source

promise.lisp.

Target Slot

state.

Function: succeed (promise &optional value)

Moves the promise to the succeeded state if possible, using the given value.

If the promise is timed out, nothing happens. If the promise is
already failed or succeeded, an error is signalled.

Returns the promise.

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: then (promise on-success)

Shorthand for attaching a success handler.

See AFTER

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: tick (promise time)

Ticks the promise to check its timeout or process handlers.

If the promise is done, all respective handlers for the promise’s state are called and popped off the promise’s handler stack. Afterwards the promise is removed from the global registry.

If the promise is pending, its timeout is checked against the given TIME (which should be a universal-time timestamp) and if timed out, is moved to the TIMEOUT state, then immediately proceeding as above.

See PROMISE
See TICK-ALL

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: tick-all (time)

Calls TICK on all registered promises.

If there were any promises to tick, T is returned, otherwise NIL.

See PROMISE
See TICK

Package

org.shirakumo.promise.

Source

promise.lisp.

Function: timeout (promise)

Moves the promise to the timed-out state if possible.

If the promise is timed out, nothing happens. If the promise is already failed or succeeded, an error is signalled.

Returns the promise.

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.


5.1.3 Generic functions

Generic Function: ensure-promise (promise-ish)

Coerces the object into a PROMISE object, if possible.

Signals an error if not possible.

The user may add methods on this function to allow integration with promise wrapper objects with the following restriction:

(eq (ensure-promise #1) (ensure-promise #1#))

Meaning calling ENSURE-PROMISE on the identical argument again must yield an identical returned PROMISE.

See PROMISE

Package

org.shirakumo.promise.

Source

promise.lisp.

Methods
Method: ensure-promise ((promise promise))

5.1.4 Standalone methods

Method: print-object ((promise promise) stream)
Source

promise.lisp.


5.1.5 Structures

Structure: promise

Represents a promise for a future value.

A promise can be in four different states:

:PENDING – The promise should be resolved in the future. :SUCCESS – The promise has successfully resolved and is done. :FAILURE – The promise has failed to resolved properly and is done. :TIMEOUT – The promise did not resolve in time and is done.

A promise can only be moved to one of the latter three states. Once done, it cannot be "re-armed". Attempting to chain another handler onto a promise that is done will cause the handler to fire immediately or not at all, depending on the type of handler. In that case the handler will be called synchronously (!) If a handler is attached to a pending promise, the handler will be called asynchronously whenever the promise moves to the requested state and TICK is called.

Handlers can be attached to a promise through AFTER.

A promise can be manually forced into one of its other states through SUCCEED, FAIL, and TIMEOUT.

Once a promise has been constructed, it is registered globally so it can be ticked through TICK-ALL. Once a promise has resolved and all its handlers have been processed through TICK, it is deregistered again, freeing it for collection.

Note that this global registration means that you MUST always resolve a promise, or in the very least always set a lifetime, or the promise will stick around in the image as garbage forever.

See STATE
See SUCCEED
See FAIL
See TIMEOUT
See MAKE
See PEND
See TICK
See TICK-ALL
See AFTER
See DONE-P

Package

org.shirakumo.promise.

Source

promise.lisp.

Direct superclasses

structure-object.

Direct methods
Direct slots
Slot: state
Type

symbol

Initform

:pending

Readers

state.

Writers

(setf state).

Slot: deadline
Type

(unsigned-byte 64)

Initform

0

Readers

deadline.

Writers

This slot is read-only.

Slot: value
Readers

value.

Writers

(setf value).

Slot: on-success
Type

list

Readers

on-success.

Writers

(setf on-success).

Slot: on-failure
Type

list

Readers

on-failure.

Writers

(setf on-failure).

Slot: on-timeout
Type

list

Readers

on-timeout.

Writers

(setf on-timeout).


5.2 Internals


5.2.1 Special variables

Special Variable: *promises*
Package

org.shirakumo.promise.

Source

promise.lisp.


5.2.2 Macros

Macro: with-promise-handling ((promise) &body body)
Package

org.shirakumo.promise.

Source

promise.lisp.


5.2.3 Ordinary functions

Function: %%make (deadline &key on-success on-failure on-timeout)
Package

org.shirakumo.promise.

Source

promise.lisp.

Function: %make (lifetime)
Package

org.shirakumo.promise.

Source

promise.lisp.

Function: chain (promise predecessor)
Package

org.shirakumo.promise.

Source

promise.lisp.

Reader: deadline (instance)
Package

org.shirakumo.promise.

Source

promise.lisp.

Target Slot

deadline.

Function: deregister (promise)
Package

org.shirakumo.promise.

Source

promise.lisp.

Reader: on-failure (instance)
Writer: (setf on-failure) (instance)
Package

org.shirakumo.promise.

Source

promise.lisp.

Target Slot

on-failure.

Reader: on-success (instance)
Writer: (setf on-success) (instance)
Package

org.shirakumo.promise.

Source

promise.lisp.

Target Slot

on-success.

Reader: on-timeout (instance)
Writer: (setf on-timeout) (instance)
Package

org.shirakumo.promise.

Source

promise.lisp.

Target Slot

on-timeout.

Function: register (promise)
Package

org.shirakumo.promise.

Source

promise.lisp.

Reader: value (instance)
Writer: (setf value) (instance)
Package

org.shirakumo.promise.

Source

promise.lisp.

Target Slot

value.


Appendix A Indexes


A.1 Concepts


A.2 Functions

Jump to:   %   (   -  
A   C   D   E   F   G   H   I   M   O   P   R   S   T   V   W  
Index Entry  Section

%
%%make: Private ordinary functions
%make: Private ordinary functions

(
(setf on-failure): Private ordinary functions
(setf on-success): Private ordinary functions
(setf on-timeout): Private ordinary functions
(setf state): Public ordinary functions
(setf value): Private ordinary functions

-
->: Public macros

A
after: Public ordinary functions
all: Public ordinary functions
any: Public ordinary functions

C
chain: Private ordinary functions
clear: Public ordinary functions

D
deadline: Private ordinary functions
deregister: Private ordinary functions
do-promised: Public macros
do-times-promised: Public macros
done-p: Public ordinary functions

E
each: Public ordinary functions
ensure-promise: Public generic functions
ensure-promise: Public generic functions

F
fail: Public ordinary functions
finally: Public ordinary functions
Function, %%make: Private ordinary functions
Function, %make: Private ordinary functions
Function, (setf on-failure): Private ordinary functions
Function, (setf on-success): Private ordinary functions
Function, (setf on-timeout): Private ordinary functions
Function, (setf state): Public ordinary functions
Function, (setf value): Private ordinary functions
Function, after: Public ordinary functions
Function, all: Public ordinary functions
Function, any: Public ordinary functions
Function, chain: Private ordinary functions
Function, clear: Public ordinary functions
Function, deadline: Private ordinary functions
Function, deregister: Private ordinary functions
Function, done-p: Public ordinary functions
Function, each: Public ordinary functions
Function, fail: Public ordinary functions
Function, finally: Public ordinary functions
Function, handle: Public ordinary functions
Function, iterate: Public ordinary functions
Function, make: Public ordinary functions
Function, on-failure: Private ordinary functions
Function, on-success: Private ordinary functions
Function, on-timeout: Private ordinary functions
Function, pend: Public ordinary functions
Function, register: Private ordinary functions
Function, state: Public ordinary functions
Function, succeed: Public ordinary functions
Function, then: Public ordinary functions
Function, tick: Public ordinary functions
Function, tick-all: Public ordinary functions
Function, timeout: Public ordinary functions
Function, value: Private ordinary functions

G
Generic Function, ensure-promise: Public generic functions

H
handle: Public ordinary functions

I
iterate: Public ordinary functions

M
Macro, ->: Public macros
Macro, do-promised: Public macros
Macro, do-times-promised: Public macros
Macro, with-handlers: Public macros
Macro, with-promise: Public macros
Macro, with-promise-handling: Private macros
make: Public ordinary functions
Method, ensure-promise: Public generic functions
Method, print-object: Public standalone methods

O
on-failure: Private ordinary functions
on-success: Private ordinary functions
on-timeout: Private ordinary functions

P
pend: Public ordinary functions
print-object: Public standalone methods

R
register: Private ordinary functions

S
state: Public ordinary functions
succeed: Public ordinary functions

T
then: Public ordinary functions
tick: Public ordinary functions
tick-all: Public ordinary functions
timeout: Public ordinary functions

V
value: Private ordinary functions

W
with-handlers: Public macros
with-promise: Public macros
with-promise-handling: Private macros