• 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并返回结果,如下例所示。
18// [Start concurrent_taskpool_promise_return]
19import { taskpool } from '@kit.ArkTS';
20
21@Concurrent
22function testPromise(args1: number, args2: number): Promise<number> {
23  return new Promise<number>((resolve, reject) => {
24    resolve(args1 + args2);
25  });
26}
27
28@Concurrent
29async function testPromise1(args1: number, args2: number): Promise<number> {
30  return new Promise<number>((resolve, reject) => {
31    resolve(args1 + args2);
32  });
33}
34
35@Concurrent
36async function testPromise2(args1: number, args2: number): Promise<number> {
37  return await new Promise<number>((resolve, reject) => {
38    resolve(args1 + args2);
39  });
40}
41
42@Concurrent
43function testPromise3() {
44  return Promise.resolve(1);
45}
46
47@Concurrent
48async function testPromise4(): Promise<number> {
49  return 1;
50}
51
52@Concurrent
53async function testPromise5(): Promise<string> {
54  return await new Promise((resolve) => {
55    setTimeout(() => {
56      resolve('Promise setTimeout after resolve');
57    }, 1000)
58  });
59}
60
61async function testConcurrentFunc() {
62  const task1: taskpool.Task = new taskpool.Task(testPromise, 1, 2);
63  const task2: taskpool.Task = new taskpool.Task(testPromise1, 1, 2);
64  const task3: taskpool.Task = new taskpool.Task(testPromise2, 1, 2);
65  const task4: taskpool.Task = new taskpool.Task(testPromise3);
66  const task5: taskpool.Task = new taskpool.Task(testPromise4);
67  const task6: taskpool.Task = new taskpool.Task(testPromise5);
68
69  taskpool.execute(task1).then((d: object) => {
70    console.info(`task1 res is: ${d}`); // 输出结果:task1 res is: 3
71  }).catch((e: object) => {
72    console.error(`task1 catch e: ${e}`);
73  })
74  taskpool.execute(task2).then((d: object) => {
75    console.info(`task2 res is: ${d}`);
76  }).catch((e: object) => {
77    console.error(`task2 catch e: ${e}`); // 输出结果:task2 catch e: Error: Can't return Promise in pending state
78  })
79  taskpool.execute(task3).then((d: object) => {
80    console.info(`task3 res is: ${d}`); // 输出结果:task3 res is: 3
81  }).catch((e: object) => {
82    console.error(`task3 catch e: ${e}`);
83  })
84  taskpool.execute(task4).then((d: object) => {
85    console.info(`task4 res is: ${d}`); // 输出结果:task4 res is: 1
86  }).catch((e: object) => {
87    console.error(`task4 catch e: ${e}`);
88  })
89  taskpool.execute(task5).then((d: object) => {
90    console.info(`task5 res is: ${d}`); // 输出结果:task5 res is: 1
91  }).catch((e: object) => {
92    console.error(`task5 catch e: ${e}`);
93  })
94  taskpool.execute(task6).then((d: object) => {
95    console.info(`task6 res is: ${d}`); // 输出结果:task6 res is: Promise setTimeout after resolve
96  }).catch((e: object) => {
97    console.error(`task6 catch e: ${e}`);
98  })
99}
100
101@Entry
102@Component
103struct Index {
104  @State message: string = 'Hello World';
105
106  build() {
107    Row() {
108      Column() {
109        Button(this.message)
110          .fontSize(50)
111          .fontWeight(FontWeight.Bold)
112          .onClick(() => {
113            testConcurrentFunc();
114            // [StartExclude update_message_on_success]
115            this.message = 'success';
116            // [EndExclude update_message_on_success]
117          })
118      }
119      .width('100%')
120    }
121    .height('100%')
122  }
123}
124// [End concurrent_taskpool_promise_return]
125