在使用 TaskPool 时,执行的并发函数需要使用该装饰器修饰,否则无法通过相关校验。
说明:
从API version 9开始,该装饰器支持在ArkTS卡片中使用。
装饰器说明
@Concurrent并发装饰器 | 说明 |
---|---|
装饰器参数 | 无。 |
使用场景 | 仅支持在Stage模型的工程中使用。 |
装饰的函数类型 | 允许标注async函数或普通函数。禁止标注generator、箭头函数、method。不支持类成员函数或者匿名函数。 |
装饰的函数内的变量类型 | 允许使用local变量、入参和通过import引入的变量。禁止使用闭包变量。 |
装饰的函数内的返回值类型 | 支持的类型请查 序列化支持类型。 |
说明:
并发函数中返回Promise的表现需关注,其中并发同步函数会处理返回该Promise并返回结果。
示例:
import taskpool from '@ohos.taskpool';
@Concurrent
function testPromise(args1: number, args2: number): Promise<number> {
return new Promise<number>((testFuncA, testFuncB)=>{
testFuncA(args1 + args2);
});
}
@Concurrent
async function testPromise1(args1: number, args2: number): Promise<number> {
return new Promise<number>((testFuncA, testFuncB)=>{
testFuncA(args1 + args2);
});
}
@Concurrent
async function testPromise2(args1: number, args2: number): Promise<number> {
return await new Promise<number>((testFuncA, testFuncB)=>{
testFuncA(args1 + args2)
});
}
@Concurrent
function testPromise3() {
return Promise.resolve(1);
}
@Concurrent
async function testPromise4(): Promise<number> {
return 1;
}
@Concurrent
async function testPromise5(): Promise<string>