• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// 并发函数返回Promise
17// 并发函数中返回Promise的表现需关注,其中并发同步函数会处理返回该Promise并返回结果,如下例所示。
18import { taskpool } from '@kit.ArkTS';
19
20@Concurrent
21function testPromise(args1: number, args2: number): Promise<number> {
22  return new Promise<number>((testFuncA, testFuncB) => {
23    testFuncA(args1 + args2);
24  });
25}
26
27@Concurrent
28async function testPromise1(args1: number, args2: number): Promise<number> {
29  return new Promise<number>((testFuncA, testFuncB) => {
30    testFuncA(args1 + args2);
31  });
32}
33
34@Concurrent
35async function testPromise2(args1: number, args2: number): Promise<number> {
36  return await new Promise<number>((testFuncA, testFuncB) => {
37    testFuncA(args1 + args2);
38  });
39}
40
41@Concurrent
42function testPromise3() {
43  return Promise.resolve(1);
44}
45
46@Concurrent
47async function testPromise4(): Promise<number> {
48  return 1;
49}
50
51@Concurrent
52async function testPromise5(): Promise<string> {
53  return await new Promise((resolve) => {
54    setTimeout(() => {
55      resolve('Promise setTimeout after resolve');
56    }, 1000)
57  });
58}
59
60async function testConcurrentFunc() {
61  let task1: taskpool.Task = new taskpool.Task(testPromise, 1, 2);
62  let task2: taskpool.Task = new taskpool.Task(testPromise1, 1, 2);
63  let task3: taskpool.Task = new taskpool.Task(testPromise2, 1, 2);
64  let task4: taskpool.Task = new taskpool.Task(testPromise3);
65  let task5: taskpool.Task = new taskpool.Task(testPromise4);
66  let task6: taskpool.Task = new taskpool.Task(testPromise5);
67
68  taskpool.execute(task1).then((d: object) => {
69    console.info('task1 res is: ' + d);
70  }).catch((e: object) => {
71    console.info('task1 catch e: ' + e);
72  })
73  taskpool.execute(task2).then((d: object) => {
74    console.info('task2 res is: ' + d);
75  }).catch((e: object) => {
76    console.info('task2 catch e: ' + e);
77  })
78  taskpool.execute(task3).then((d: object) => {
79    console.info('task3 res is: ' + d);
80  }).catch((e: object) => {
81    console.info('task3 catch e: ' + e);
82  })
83  taskpool.execute(task4).then((d: object) => {
84    console.info('task4 res is: ' + d);
85  }).catch((e: object) => {
86    console.info('task4 catch e: ' + e);
87  })
88  taskpool.execute(task5).then((d: object) => {
89    console.info('task5 res is: ' + d);
90  }).catch((e: object) => {
91    console.info('task5 catch e: ' + e);
92  })
93  taskpool.execute(task6).then((d: object) => {
94    console.info('task6 res is: ' + d);
95  }).catch((e: object) => {
96    console.info('task6 catch e: ' + e);
97  })
98}
99
100@Entry
101@Component
102struct Index {
103  @State message: string = 'Hello World';
104
105  build() {
106    Row() {
107      Column() {
108        Button(this.message)
109          .fontSize(50)
110          .fontWeight(FontWeight.Bold)
111          .onClick(() => {
112            testConcurrentFunc();
113            this.message = 'success';
114          })
115      }
116      .width('100%')
117    }
118    .height('100%')
119  }
120}
121