1# 验证示例代码同源--异常场景1 2 3 4## ID无嵌套,存在空行 5 6代码不一致(空行) 7```ts 8const promise: Promise<number> = new Promise((resolve: Function, reject: Function) => { 9 setTimeout(() => { 10 const randomNumber: number = Math.random(); 11 if (randomNumber > 0.5) { 12 resolve(randomNumber); 13 14 } else { 15 reject(new Error('Random number is too small')); 16 } 17 }, 1000); 18}) 19``` 20<!--@[promise_async_operation](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/AsyncConcurrencyOverview/entry/src/main/ets/pages/Index.ets)--> 21 22## docs文档中的ID在Sample中不存在(不匹配) 23 24```ts 25const promise: Promise<number> = new Promise((resolve: Function, reject: Function) => { 26 setTimeout(() => { 27 const randomNumber: number = Math.random(); 28 if (randomNumber > 0.5) { 29 resolve(randomNumber); 30 } else { 31 reject(new Error('Random number is too small')); 32 } 33 }, 1000); 34}) 35``` 36<!--@[code-null](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/MultithreadedConcurrency/MultiThreadConcurrencyOverview/entry/src/main/ets/pages/Index.ets)--> 37 38 39## 引用部分代码(Exclude字段) 40 41### 注释不一致(新增注释),代码内容一致 42 43```ts 44//新增注释 45// 开始--async_await_sync_operation 46async function myAsyncFunction(): Promise<string> { 47 const result: string = await new Promise((resolve: Function) => { 48 setTimeout(() => { 49 resolve('Hello, world!'); 50 }, 3000); 51 }); 52 console.info(result); // 输出: Hello, world! 53 return result; 54} 55 56@Entry 57@Component 58struct PromiseAsyncAwait { 59 @State message: string = 'Hello World'; 60 61 build() { 62 Row() { 63 Column() { 64 Text(this.message) 65 .fontSize(50) 66 .fontWeight(FontWeight.Bold) 67 .onClick(async () => { 68 let res = await myAsyncFunction(); 69 console.info('res is: ' + res); 70 }) 71 } 72 .width('100%') 73 } 74 .height('100%') 75 } 76} 77// 结束--async_await_sync_operation 78``` 79<!--@[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)--> 80 81### docs代码中未去除Exclude字段 82 83```ts 84import { taskpool } from '@kit.ArkTS'; 85 86// 跨线程并发任务 87@Concurrent 88async function produce(): Promise<number> { 89 // 添加生产相关逻辑 90 console.info('producing...'); 91 return Math.random(); 92} 93 94class Consumer { 95 public consume(value: Object) { 96 // 添加消费相关逻辑 97 console.info('consuming value: ' + value); 98 } 99} 100 101@Entry 102@Component 103struct Index { 104 @State message: string = 'Hello World'; 105 106 build() { 107 Row() { 108 Column() { 109 Text(this.message) 110 .fontSize(50) 111 .fontWeight(FontWeight.Bold) 112 Button() { 113 Text('start') 114 }.onClick(() => { 115 let produceTask: taskpool.Task = new taskpool.Task(produce); 116 let consumer: Consumer = new Consumer(); 117 for (let index: number = 0; index < 10; index++) { 118 // 执行生产异步并发任务 119 taskpool.execute(produceTask).then((res: Object) => { 120 consumer.consume(res); 121 }).catch((e: Error) => { 122 console.error(e.message); 123 }) 124 } 125 this.message = 'success'; 126 }) 127 .id('button') 128 .width('20%') 129 .height('20%') 130 } 131 .width('100%') 132 } 133 .height('100%') 134 } 135} 136``` 137<!--@[actor_model](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/MultithreadedConcurrency/MultiThreadConcurrencyOverview/entry/src/main/ets/pages/Index.ets)--> 138 139 140## 嵌套的ID,docs中不含嵌套部分(修改4的部分,是否因为空行导致的报错,此文档中,已经取消了空行) 141 142```ts 143import { BusinessError } from '@kit.BasicServicesKit'; 144promise.then((result: number) => { 145 console.info(`Random number is ${result}`); 146}).catch((error: BusinessError) => { 147 console.error(error.message); 148}); 149``` 150<!--@[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)-->