abapify/promise
A Promise and async/await implementation for ABAP, modelled on JavaScript's Promise API, that runs a resolver as a background task and offers all, race, resolve, reject and all_settled.
Creating a zcl_promise with a resolver runs that resolver as a serialised asynchronous task in the background, so the rest of the calling program keeps running instead of waiting on it immediately; a handler's on_fulfilled or on_rejected method, or an await call inside a class that inherits from zcl_abap_async, picks up the result once the task resolves or rejects. Promise=>all combines several promises into one that resolves with all their results, or rejects with the reason of whichever one rejects first, and the same class exposes race, resolve, reject and all_settled. The maintainer notes the benefit is small for a single promise and grows for a promise=>all call across several.
Facts
Where it sits
Who is behind it
In the atlas: maintains 9@abapifymaintainerPetr PlenkovContributes to this
In the atlas: contributes to 13@ThePlenkov1 contributor
Before choosing this
Signals
ActivityQuietNo published releases; repository quiet for 36 months
It publishes no releases, and its repository has been quiet for at least 24 full months. This may be normal for stable software; check whether its current state still fits your use.
What it declares
Every artifact
- Type
- Package
- Origin
- Independently maintained
As a package
- Where it runs
- does not say
- Distributed through
- does not say
Connections
Splitting one run's work across several work processes
Download
README
quoted from abapify/promise under MIT, readLet's make ABAP async!
To understand how this class work use this as a reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
How to use
Promise
what runs in js
new Promise( ( resolve, reject ) => { resolve( 'OK' ) } ).then( ( value ) => console.log( value ) ) // OK
now runs in ABAP ( with a bit more lines of course )
REPORT ZTEST_PROMISE_OK.
" Promise Resolver
" Must implement then method with mandatory resolve or reject method calls from inside
CLASS lcl_ok DEFINITION INHERITING FROM zcl_promise_resolver.
PUBLIC SECTION.
METHODS then REDEFINITION.
ENDCLASS.
CLASS lcl_ok IMPLEMENTATION.
METHOD then.
" resolve is declared in zcl_promise_resolver
resolve( |OK| ).
ENDMETHOD.
ENDCLASS.
" Promise handler
" has on_fulfilled and on_rejected callbacks with default empty implementation
CLASS lcl_ok_handler DEFINITION INHERITING FROM zcl_promise_handler.
PUBLIC SECTION.
METHODS ON_FULFILLED REDEFINITION.
ENDCLASS.
CLASS lcl_ok_handler IMPLEMENTATION.
METHOD on_fulfilled.
" ideally resolver needs to know which type it expects to have as a result
write cast string( with )->*.
ENDMETHOD.
endclass.
START-OF-SELECTION.
new zcl_promise( new lcl_ok( ) )->then( new lcl_ok_handler( ) ).
So what's the purpose?
You probably think, why we wouldn't just run write 'OK'
The answer is in the following sequence diagram
What promise class does during inititalization - is creating an asyncronous task with serialized resolver instance. Asyncronous task deserialises resolver and executes it. It may be time consuming operation, for example some calculation, query, or http request. So why won't let the rest of abap code work for something else? So when your code decides that it's time to get data from promise and only then actually triggers for wait statement. Once an asynctronous task is done by triggering resolve/reject method in your resolver instance - it will serialize again the response and send back to your main session.
You may not see the desired effect for single operations - but for promise=>all call the effect can be huge!
And what about async/await?
To use full power of promises we can also use await functions.
So mentioned above example may look a bit differently:
async function() {
console.log( await new Promise( ( resolve, reject ) => { resolve( 'OK' ) } ) ) // OK
}
now works in ABAP
REPORT ZTEST_PROMISE_OK_AWAIT.
" Promise Resolver
" Must implement then method with mandatory resolve or reject method calls from inside
CLASS lcl_ok DEFINITION INHERITING FROM zcl_promise_resolver.
PUBLIC SECTION.
METHODS then REDEFINITION.
ENDCLASS.
CLASS lcl_ok IMPLEMENTATION.
METHOD then.
" resolve is declared in zcl_promise_resolver
resolve( |OK| ).
ENDMETHOD.
ENDCLASS.
CLASS lcl_app DEFINITION INHERITING FROM zcl_abap_async.
PUBLIC SECTION.
METHODs start.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD start.
" we expect result as ref to string
write cast string(
" in case if it's async class ( child of async )
" you can just use await of any promise
await( new zcl_promise( new lcl_ok( ) ) ) )->*.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
" start the app
new lcl_app( )->start( ).
Other methods
Promise=>all
Returns a new promise, resolved with array of promises results or rejected with a reason of the first rejected promise
Promise=>race
Promise=>resolve
Returns resolved promise
Promise=>reject
Returns rejected promise
Promise=>all_settled
Others providing
Compare all 6 on the Parallel processing page →- ABAPParallelizationServiceABAPParallelizationServiceA generic framework, called from code, for running a function unit, report or method across many parameter combinations in parallel dialog or batch tasks.independent, abapFramework for running ABAP work units in parallel tasksfrom ABAP
- COROUTINES4ABAPCOROUTINES4ABAPA coroutine framework for ABAP that launches a piece of work, such as a database query or a background task, as a task the caller awaits later while carrying on with other work.independent, abapCoroutine framework for ABAP with awaitable resultsfrom ABAP
- zconcurrency_apizconcurrency_apiA Java-inspired ABAP library that runs tasks in parallel over SAP's SPTA framework without an RFC-enabled function module, capping concurrent tasks at 40% of the dialog work processes.independent, abapParallel task library for ABAP over the SPTA frameworkfrom ABAP
- RFC_TOOLSRFC_TOOLSA wrapper class for calling asynchronous RFC function modules in parallel from ABAP, one instance per parallel task.independent, abapA wrapper class for calling asynchronous RFC function modules in parallel from ABAP, one instance per parallel task.from ABAP
- zthreadzthreadA threading library for ABAP modelled on Java's Thread and Runnable interface: a runnable class is forked as a thread, joined singly or all at once, and returns a result or a callback.independent, abapThreading library for ABAP modelled on Java's Thread and Runnablefrom ABAP
Details
Sources
Sources are graph entities: the same page may back several entries, and an unreachable source flags every row that rests on it.
Compatibility and provenance
abaplint
Contributors
History in the atlas
- First seen.
For your agent
- Ref
artifact:abapify-promise- JSON
- /artifacts/abapify-promise.json
- MCP
- Connecting an agent