1# \@Concurrent装饰器:声明并校验并发函数 2 3在使用[TaskPool](../reference/apis-arkts/js-apis-taskpool.md)时,执行的并发函数需要使用该装饰器修饰,否则无法通过相关校验。 4 5 6> **说明:** 7> 8> 从API version 9开始,该装饰器支持在ArkTS卡片中使用。 9 10 11## 装饰器说明 12| \@Concurrent并发装饰器 | 说明 | 13| --------------------- | ------------------------------------------------------------------------------------------ | 14| 装饰器参数 | 无。 | 15| 使用场景 | 仅支持在Stage模型的工程中使用。 | 16| 装饰的函数类型 | 允许标注async函数或普通函数。禁止标注generator、箭头函数、method。不支持类成员函数或者匿名函数。 | 17| 装饰的函数内的变量类型 | 允许使用local变量、入参和通过import引入的变量。禁止使用闭包变量。 | 18| 装饰的函数内的返回值类型 | 支持的类型请查[序列化支持类型](serialization-support-types.md)。 | 19 20> **说明:** 21> 22> 并发函数中返回Promise的表现需关注,其中并发同步函数会处理返回该Promise并返回结果。 23 24**示例:** 25 26```ts 27import taskpool from '@ohos.taskpool'; 28 29@Concurrent 30function testPromise(args1: number, args2: number): Promise<number> { 31 return new Promise<number>((testFuncA, testFuncB)=>{ 32 testFuncA(args1 + args2); 33 }); 34} 35 36@Concurrent 37async function testPromise1(args1: number, args2: number): Promise<number> { 38 return new Promise<number>((testFuncA, testFuncB)=>{ 39 testFuncA(args1 + args2); 40 }); 41} 42 43@Concurrent 44async function testPromise2(args1: number, args2: number): Promise<number> { 45 return await new Promise<number>((testFuncA, testFuncB)=>{ 46 testFuncA(args1 + args2) 47 }); 48} 49 50@Concurrent 51function testPromise3() { 52 return Promise.resolve(1); 53} 54 55@Concurrent 56async function testPromise4(): Promise<number> { 57 return 1; 58} 59 60@Concurrent 61async function testPromise5(): Promise<string> { 62 return await new Promise((resolve) => { 63 setTimeout(()=>{ 64 resolve("Promise setTimeout after resolve"); 65 }, 1000) 66 }); 67} 68 69async function testConcurrentFunc() { 70 let task1: taskpool.Task = new taskpool.Task(testPromise, 1, 2); 71 let task2: taskpool.Task = new taskpool.Task(testPromise1, 1, 2); 72 let task3: taskpool.Task = new taskpool.Task(testPromise2, 1, 2); 73 let task4: taskpool.Task = new taskpool.Task(testPromise3); 74 let task5: taskpool.Task = new taskpool.Task(testPromise4); 75 let task6: taskpool.Task = new taskpool.Task(testPromise5); 76 77 taskpool.execute(task1).then((d:object)=>{ 78 console.info("task1 res is: " + d) 79 }).catch((e:object)=>{ 80 console.info("task1 catch e: " + e) 81 }) 82 taskpool.execute(task2).then((d:object)=>{ 83 console.info("task2 res is: " + d) 84 }).catch((e:object)=>{ 85 console.info("task2 catch e: " + e) 86 }) 87 taskpool.execute(task3).then((d:object)=>{ 88 console.info("task3 res is: " + d) 89 }).catch((e:object)=>{ 90 console.info("task3 catch e: " + e) 91 }) 92 taskpool.execute(task4).then((d:object)=>{ 93 console.info("task4 res is: " + d) 94 }).catch((e:object)=>{ 95 console.info("task4 catch e: " + e) 96 }) 97 taskpool.execute(task5).then((d:object)=>{ 98 console.info("task5 res is: " + d) 99 }).catch((e:object)=>{ 100 console.info("task5 catch e: " + e) 101 }) 102 taskpool.execute(task6).then((d:object)=>{ 103 console.info("task6 res is: " + d) 104 }).catch((e:object)=>{ 105 console.info("task6 catch e: " + e) 106 }) 107} 108 109testConcurrentFunc(); 110``` 111 112输出结果如下所示: 113``` 114task1 res is: 3 115task2 catch e: Error: Can't return Promise in pending state 116task3 res is: 3 117task4 res is: 1 118task5 res is: 1 119task6 res is: Promise setTimeout after resolve 120``` 121 122> **说明:** 123> 124> 并发异步方法中如果使用Promise,建议搭配await使用捕获Promise中可能发生的异常。推荐使用示例如下。 125 126**示例:** 127 128```ts 129@Concurrent 130async function testPromiseError() { 131 await new Promise<number>((resolve, reject) => { 132 resolve(1); 133 }).then(()=>{ 134 throw new Error("testPromise Error"); 135 }) 136} 137 138@Concurrent 139async function testPromiseError1() { 140 await new Promise<string>((resolve, reject) => { 141 reject("testPromiseError1 Error msg"); 142 }) 143} 144 145@Concurrent 146function testPromiseError2() { 147 return new Promise<string>((resolve, reject) => { 148 reject("testPromiseError2 Error msg"); 149 }) 150} 151 152async function testConcurrentFunc() { 153 let task1: taskpool.Task = new taskpool.Task(testPromiseError); 154 let task2: taskpool.Task = new taskpool.Task(testPromiseError1); 155 let task3: taskpool.Task = new taskpool.Task(testPromiseError2); 156 157 taskpool.execute(task1).then((d:object)=>{ 158 console.info("task1 res is: " + d) 159 }).catch((e:object)=>{ 160 console.info("task1 catch e: " + e) 161 }) 162 taskpool.execute(task2).then((d:object)=>{ 163 console.info("task2 res is: " + d) 164 }).catch((e:object)=>{ 165 console.info("task2 catch e: " + e) 166 }) 167 taskpool.execute(task3).then((d:object)=>{ 168 console.info("task3 res is: " + d) 169 }).catch((e:object)=>{ 170 console.info("task3 catch e: " + e) 171 }) 172} 173 174testConcurrentFunc() 175``` 176 177输出结果如下所示: 178``` 179task1 catch e: Error: testPromise Error 180task2 catch e: testPromiseError1 Error msg 181task3 catch e: testPromiseError2 Error msg 182``` 183 184 185## 装饰器使用示例 186 ```ts 187 import taskpool from '@ohos.taskpool'; 188 189 @Concurrent 190 function add(num1: number, num2: number): number { 191 return num1 + num2; 192 } 193 194 async function ConcurrentFunc(): Promise<void> { 195 try { 196 let task: taskpool.Task = new taskpool.Task(add, 1, 2); 197 console.info("taskpool res is: " + await taskpool.execute(task)); 198 } catch (e) { 199 console.error("taskpool execute error is: " + e); 200 } 201 } 202 203 @Entry 204 @Component 205 struct Index { 206 @State message: string = 'Hello World' 207 208 build() { 209 Row() { 210 Column() { 211 Text(this.message) 212 .fontSize(50) 213 .fontWeight(FontWeight.Bold) 214 .onClick(() => { 215 ConcurrentFunc(); 216 }) 217 } 218 .width('100%') 219 } 220 .height('100%') 221 } 222 } 223 ```