• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 验证示例代码同源--异常场景5
2
3
4## 文档代码块中,id重复(但是引用的内容一致),验证扫描结果
5
6doc此段代码将ID写错了
7
8```ts
9const promise: Promise<number> = new Promise((resolve: Function, reject: Function) => {
10  setTimeout(() => {
11    const randomNumber: number = Math.random();
12    if (randomNumber > 0.5) {
13      resolve(randomNumber);
14    } else {
15      reject(new Error('Random number is too small'));
16    }
17  }, 1000);
18})
19```
20<!--@[async_await_sync_operation](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/AsyncConcurrencyOverview/entry/src/main/ets/pages/Index.ets)-->
21
22
23```
24async function myAsyncFunction(): Promise<string> {
25  const result: string = await new Promise((resolve: Function) => {
26    setTimeout(() => {
27      resolve('Hello, world!');
28    }, 3000);
29  });
30  console.info(result); // 输出: Hello, world!
31  return result;
32}
33
34@Entry
35@Component
36struct PromiseAsyncAwait {
37  @State message: string = 'Hello World';
38
39  build() {
40    Row() {
41      Column() {
42        Text(this.message)
43          .fontSize(50)
44          .fontWeight(FontWeight.Bold)
45          .onClick(async () => {
46            let res = await myAsyncFunction();
47            console.info('res is: ' + res);
48          })
49      }
50      .width('100%')
51    }
52    .height('100%')
53  }
54}
55```
56<!--@[async_await_sync_operation](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/AsyncConcurrencyOverview/entry/src/main/ets/pages/Index.ets)-->
57
58
59
60## 嵌套的ID,docs中不含嵌套部分
61
62```ts
63import { BusinessError } from '@kit.BasicServicesKit';
64promise.then((result: number) => {
65  console.info(`Random number is ${result}`);
66}).catch((error: BusinessError) => {
67  console.error(error.message);
68});
69```
70<!--@[promise_then_catch_handling](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/AsyncConcurrencyOverview/entry/src/main/ets/pages/Index.ets)-->