• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 验证示例代码同源--异常场景4
2
3## ID无嵌套,代码不一致
4
5代码不一致(docs中只有结束的```)
6
7const promise: Promise\<number\> = new Promise((resolve: Function, reject: Function) => {
8  setTimeout(() => {
9    const randomNumber: number = Math.random();
10    if (randomNumber > 0.5) {
11      resolve(randomNumber);
12    } else {
13      reject(new Error('Random number is too small'));
14    }
15  },  1000);
16})
17```
18<!--@[Start promise_async_operation](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/MultithreadedConcurrency/MultiThreadConcurrencyOverview/entry/src/main/ets/pages/Index.ets)-->
19
20
21代码不一致(docs中只有开始的```)
22
23```
24const promise: Promise\<number\> = new Promise((resolve: Function, reject: Function) => {
25  setTimeout(() => {
26    const randomNumber: number = Math.random();
27    if (randomNumber > 0.5) {
28      resolve(randomNumber);
29    } else {
30      reject(new Error('Random number is too small'));
31    }
32  },  1000);
33})
34
35<!--@[Start promise_async_operation](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/MultithreadedConcurrency/MultiThreadConcurrencyOverview/entry/src/main/ets/pages/Index.ets)-->
36
37## 嵌套的ID,docs中不含嵌套部分
38
39```ts
40import { BusinessError } from '@kit.BasicServicesKit';
41
42
43
44promise.then((result: number) => {
45  console.info(`Random number is ${result}`);
46}).catch((error: BusinessError) => {
47  console.error(error.message);
48});
49```
50<!--@[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)-->
51
52
53## 注释不一致(docs:行上 vs Sample:行),代码内容一致
54
55```ts
56async function myAsyncFunction(): Promise<string> {
57    const result: string = await new Promise((resolve: Function) => {
58    setTimeout(() => {
59        resolve('Hello, world!');
60    }, 3000);
61    });
62    // 输出: Hello, world!
63    console.info(result);
64    return result;
65}
66
67@Entry
68@Component
69struct PromiseAsyncAwait {
70    @State message: string = 'Hello World';
71
72    build() {
73    Row() {
74        Column() {
75        Text(this.message)
76            .fontSize(50)
77            .fontWeight(FontWeight.Bold)
78            .onClick(async () => {
79            let res = await myAsyncFunction();
80            console.info('res is: ' + res);
81            })
82        }
83        .width('100%')
84    }
85    .height('100%')
86    }
87}
88```
89<!--@[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)-->
90
91### sample代码,start和end未同时出现
92
93```ts
94const promise: Promise<number> = new Promise((resolve: Function, reject: Function) => {
95  setTimeout(() => {
96    const randomNumber: number = Math.random();
97    if (randomNumber > 0.5) {
98      resolve(randomNumber);
99    } else {
100      reject(new Error('Random number is too small'));
101    }
102  }, 1000);
103})
104```
105<!--@[test-promise_async_operation](https://gitcode.com/albee1213/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/MultithreadedConcurrency/TaskPoolIntroduction/entry/src/main/ets/managers/asynchronousfunctions.ets)-->
106
107
108```ts
109async function myAsyncFunction(): Promise<string> {
110  const result: string = await new Promise((resolve: Function) => {
111  });
112  console.info(result); // 输出: Hello, world!
113  return result;
114}
115
116@Entry
117@Component
118struct PromiseAsyncAwait {
119  @State message: string = 'Hello World';
120
121  build() {
122    Row() {
123      Column() {
124        Text(this.message)
125          .onClick(async () => {
126            let res = await myAsyncFunction();
127            console.info('res is: ' + res);
128          })
129      }
130      .width('100%')
131    }
132    .height('100%')
133  }
134}
135```
136<!--@[test-async_await_sync_operation](https://gitcode.com/albee1213/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/MultithreadedConcurrency/TaskPoolIntroduction/entry/src/main/ets/managers/asynchronousfunctions.ets)-->