1# 验证示例代码同源--正常场景 2 3前提:同一文档中,如果出现多次引用同一段代码,则引用代码必须完全相同 4 5## 引用全量代码 6 7### ID无嵌套,代码完全一致 8 9```ts 10const promise: Promise<number> = new Promise((resolve: Function, reject: Function) => { 11 setTimeout(() => { 12 const randomNumber: number = Math.random(); 13 if (randomNumber > 0.5) { 14 resolve(randomNumber); 15 } else { 16 reject(new Error('Random number is too small')); 17 } 18 }, 1000); 19}) 20``` 21<!--@[promise_async_operation](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/AsyncConcurrencyOverview/entry/src/main/ets/pages/Index.ets)--> 22 23 24### ID无嵌套,整段代码缩进不一致 25 26* list 1 27* list 2 28 ```ts 29 const promise: Promise<number> = new Promise((resolve: Function, reject: Function) => { 30 setTimeout(() => { 31 const randomNumber: number = Math.random(); 32 if (randomNumber > 0.5) { 33 resolve(randomNumber); 34 } else { 35 reject(new Error('Random number is too small')); 36 } 37 }, 1000); 38 }) 39 ``` 40 <!--@[promise_async_operation](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/AsyncConcurrencyOverview/entry/src/main/ets/pages/Index.ets)--> 41* list 3 42 43### ID存在嵌套 44 45```ts 46import { BusinessError } from '@kit.BasicServicesKit'; 47 48const promise: Promise<number> = new Promise((resolve: Function, reject: Function) => { 49 setTimeout(() => { 50 const randomNumber: number = Math.random(); 51 if (randomNumber > 0.5) { 52 resolve(randomNumber); 53 } else { 54 reject(new Error('Random number is too small')); 55 } 56 }, 1000); 57}) 58 59promise.then((result: number) => { 60 console.info(`Random number is ${result}`); 61}).catch((error: BusinessError) => { 62 console.error(error.message); 63}); 64``` 65<!--@[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)--> 66 67 68## 引用部分代码(Exclude字段) 69 70### 内容完全一致 71 72```ts 73import { taskpool } from '@kit.ArkTS'; 74 75// 跨线程并发任务 76@Concurrent 77async function produce(): Promise<number> { 78 // 添加生产相关逻辑 79 console.info('producing...'); 80 return Math.random(); 81} 82 83class Consumer { 84 public consume(value: Object) { 85 // 添加消费相关逻辑 86 console.info('consuming value: ' + value); 87 } 88} 89 90@Entry 91@Component 92struct Index { 93 @State message: string = 'Hello World'; 94 95 build() { 96 Row() { 97 Column() { 98 Text(this.message) 99 .fontSize(50) 100 .fontWeight(FontWeight.Bold) 101 Button() { 102 Text('start') 103 }.onClick(() => { 104 let produceTask: taskpool.Task = new taskpool.Task(produce); 105 let consumer: Consumer = new Consumer(); 106 for (let index: number = 0; index < 10; index++) { 107 // 执行生产异步并发任务 108 taskpool.execute(produceTask).then((res: Object) => { 109 consumer.consume(res); 110 }).catch((e: Error) => { 111 console.error(e.message); 112 }) 113 } 114 }) 115 .width('20%') 116 .height('20%') 117 } 118 .width('100%') 119 } 120 .height('100%') 121 } 122} 123``` 124<!--@[actor_model](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTs/ArkTsConcurrent/MultithreadedConcurrency/MultiThreadConcurrencyOverview/entry/src/main/ets/pages/Index.ets)-->