lang — language utilities
The lang module offers small helpers such as delay (timed Promise), TypeScript-only lazy, lazyProp, and promise for engine lifecycle–friendly Promises.
Table of contents
Classes
Interfaces
Functions
Functions
delay
delay(timeout: number): Promise<void>
Returns a Promise that resolves after timeout milliseconds. For example, await delay(1000) continues about one second later. Unlike Pro 8’s sleep(), this does not block the JS thread or the Node event loop.
Example
"nodejs";
const { delay } = require('lang');
async function main() {
console.log(1);
await delay(2000);
console.log(2);
}
main();Parameters
| Name | Type | Description |
|---|---|---|
timeout | number | Delay in ms. If timeout <= 0, resolves on the next microtask tick (immediately). |
Returns
Promise<void>
lazy
lazy(target: any, name: string, __namedParameters?: PropertyDescriptor): any
TypeScript decorator only — not usable from plain JavaScript.
Use on a class getter: the getter body runs once, caches the return value, and subsequent reads reuse the cache.
Example
import { lazy } from 'lang'
class Sum {
private n: number;
constructor(n: number) {
this.n = n;
}
@lazy
get sum() {
console.log('calculating sum...');
let result = 0;
for (let i = 0; i < this.n; i++) {
result += i;
}
return result;
}
}
const sum = new Sum(10);
console.log(sum.sum); // calculating sum...55
console.log(sum.sum); // 55Parameters
| Name | Type | Description |
|---|---|---|
target | any | Prototype / instance passed by the decorator runtime. |
name | string | Property key. |
__namedParameters | PropertyDescriptor | Descriptor supplied by the decorator API. |
Returns
any
lazyProp
lazyProp<T>(evaluator: () => T): ReadOnlyProperty<T>Builds a ReadOnlyProperty whose value is computed by evaluator on first read (lazy), then memoized.
Type parameters
| Name |
|---|
T |
Parameters
| Name | Type | Description |
|---|---|---|
evaluator | () => T | Produces the property value (called once until invalidated by the implementation, if ever). |
Returns
ReadOnlyProperty (generic T).
promise
promise<T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>
Like new Promise(executor), but while the promise stays pending (before resolve / reject), Auto.js keeps the script engine from exiting early.
Use this when wrapping Java-side async callbacks: Node does not “see” those callbacks, so a bare new Promise might let the main script finish and tear down before the Java listener fires.
Example
"nodejs";
const { promise } = require('lang');
const { android } = require('android');
function loadAudioAsync(file) {
const SoundPool = android.media.SoundPool;
return promise(function (resolve) {
const soundPool = new SoundPool(1, SoundPool.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener($autojs.java.wrap(() => resolve(soundPool)));
soundPool.load(file, 1);
});
}Type parameters
| Name |
|---|
T |
Parameters
| Name | Type | Description |
|---|---|---|
executor | (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void | Same contract as the Promise constructor. |
Returns
Promise<T>
