lang
CloudControl Pro 9 Docs / lang
lang
lang provides language-related API, such as delay
.
Table of contents
Classes
Interfaces
Functions
Functions
delay
▸ delay(timeout
): Promise
<void
>
Returns a Promise, which will resolve after timeout. For example, await delay(1000)
will resolve after 1 second. This function does not block the thread/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 |
timeout, if less than or equal to 0, then the function returns a Promise that resolves immediately. |
Returns
Promise
<void
>
lazy
▸ lazy(target
, name
, __namedParameters?
): any
TypeScript decorator, which cannot be used in JavaScript.
Decorates class's getter properties, and the decorated properties will be automatically executed and saved the value on the first call, and then use the saved value on subsequent calls.
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); // 55
Parameters
Name | Type |
---|---|
target |
any |
name |
string |
__namedParameters |
PropertyDescriptor |
Returns
any
lazyProp
▸ lazyProp<T
>(evaluator
): ReadOnlyProperty
<T
>
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
evaluator |
() => T |
Returns
promise
▸ promise<T
>(executor
): Promise
<T
>
Creates a Promise, which is similar to new Promise
, but it will keep the engine running when the Promise is in the pending state (before resolve/reject).
Because nodejs does not know the asynchronous behavior of Java APIs, this function is usually used to create a Promise for a Java API, and keep the engine running until the Java API returns.
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 |
---|---|
executor |
(resolve : (value : T | PromiseLike <T >) => void , reject : (reason? : any ) => void ) => void |
Returns
Promise
<T
>