Options
All
  • Public
  • Public/Protected
  • All
Menu

Module lang

lang provides language-related API, such as delay.

Index

Classes

Interfaces

Functions

Functions

delay

  • delay(timeout: number): 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

    • 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: any, name: string, __namedParameters?: PropertyDescriptor): 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

    • target: any
    • name: string
    • __namedParameters: PropertyDescriptor = {}

    Returns any

lazyProp

  • Type parameters

    • T

    Parameters

    • evaluator: () => T
        • (): T
        • Returns T

    Returns ReadOnlyProperty<T>

promise

  • promise<T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): 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

    • T

    Parameters

    • executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void
        • (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void): void
        • Parameters

          • resolve: (value: T | PromiseLike<T>) => void
              • (value: T | PromiseLike<T>): void
              • Parameters

                • value: T | PromiseLike<T>

                Returns void

          • reject: (reason?: any) => void
              • (reason?: any): void
              • Parameters

                • Optional reason: any

                Returns void

          Returns void

    Returns Promise<T>