index.uts 2.0 KB
Newer Older
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
1 2 3 4 5 6
type AsyncOptions = {
  type: string;
  success: (res: string) => void;
  fail: (res: string) => void;
  complete: (res: string) => void;
};
7 8 9 10 11 12 13 14 15 16

type SyntaxResult = {
	name: string
};

type SyncResult = {
	msg: string
}


DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
17 18 19 20 21 22 23 24 25
/**
 * 导出一个属性
 */
export const MAX = 100;

/**
 * 导出一个同步方法
 * @returns
 */
26
export function testSync(msg: string): SyncResult {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
27
  console.log("log test");
28 29 30 31
  const res: SyncResult = {
	  msg: `hello ${msg}`
  }
  return res
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
}
/**
 * 导出一个同步方法(触发了数组越界异常)
 */
export function testSyncError() {
  const arr: string[] = [];
  console.log(arr[1]);
}
/**
 * 导出一个带callback的同步方法
 * @param opts
 */
export function testSyncWithCallback(opts: AsyncOptions) {
  if (opts.type == "success") {
    opts.success("success");
  } else {
    opts.fail("fail");
  }
  opts.complete("complete");
51 52 53 54 55
  
  const res: SyntaxResult = {
	  name: "testSyncWithCallback"
  }
  return res
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
56 57 58 59 60
}
/**
 * 导出一个异步方法
 * @returns
 */
61
export async function testAsync(opts: AsyncOptions): Promise<SyntaxResult> {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
62 63 64 65 66 67
  if (opts.type == "success") {
    opts.success("success");
  } else {
    opts.fail("fail");
  }
  opts.complete("complete");
68 69 70 71 72
  
  const res: SyntaxResult = {
  	  name: "testAsync"
  }
  return res
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
}

type TestOptions = {
  name: string;
  callback: (res: string) => void;
};

export class Test {
  id: number;
  name: string;
  static type: string = "Test";
  constructor(id: number, options: TestOptions) {
    this.id = id;
    this.name = options.name;
    options.callback("Test.constructor");
  }
89
  static testClassStaticSyncWithCallback(opts: AsyncOptions): SyntaxResult {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
90 91
    return testSyncWithCallback(opts);
  }
92
  static async testClassStaticAsync(opts: AsyncOptions): Promise<SyntaxResult> {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
93 94 95
    const res = await testAsync(opts);
    return res;
  }
96
  testClassSyncWithCallback(opts: AsyncOptions): SyntaxResult {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
97 98
    return testSyncWithCallback(opts);
  }
99
  async testClassAsync(opts: AsyncOptions): Promise<SyntaxResult> {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
100 101 102 103
    const res = await testAsync(opts);
    return res;
  }
}