• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ArkTS使用Node-API接口进行异步任务开发
2
3### 介绍
4
5本工程展示了使用Node-API接口进行异步任务开发中的Promise方式和callback方式,详细描述可查如下链接。
6
7- [使用Node-API接口进行异步任务开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-napi-asynchronous-task.md)
8
9### 效果预览
10
11| 首页                                                         | 执行结果图                                                   |
12| ------------------------------------------------------------ | ------------------------------------------------------------ |
13| <img src="./screenshots/NodeAPIAsynchronousTask1.png" style="zoom: 67%;" /> | <img src="./screenshots/NodeAPIAsynchronousTask2.png" style="zoom:67%;" /> |
14
15### 使用说明
16
171. 运行Index主界面。
182. 页面呈现如上图效果,和两个按钮,依次点击按钮后可以调用Promise方式和callback方式的方法,并将结果呈现到text文本中,并在控制台中打印出对应日志。
193. 运行测试用例NodeAPIAsynchronousTask.test.ets文件对页面代码进行测试可以全部通过。
20
21### 工程目录
22
23```
24entry/src/
25 ├── main
26 │   ├── cpp
27 │   │   ├── types
28 │   │       ├── libentry
29 │   │       └── libentry1
30 │   │   ├── callback.cpp                // callback方式实现
31 │   │   ├── CMakeLists.txt
32 │   │   ├── napi_init.cpp               // Promise方式实现
33 │   ├── ets
34 │   │   ├── entryability
35 │   │   ├── entrybackupability
36 │   │   ├── pages
37 │   │       ├── Index.ets               // 异步任务
38 │   ├── module.json5
39 │   └── resources
40 ├── ohosTest
41 │   ├── ets
42 │   │   └── test
43 │   │       ├── Ability.test.ets
44 │   │       ├── NodeAPIAsynchronousTask.test.ets  // 自动化测试代码
45 │   │       └── List.test.ets
46```
47
48### 相关权限
49
50不涉及。
51
52### 依赖
53
54不涉及。
55
56### 约束与限制
57
581.本示例仅支持标准系统上运行, 支持设备:RK3568。
59
602.本示例为Stage模型,支持API14版本SDK,版本号:5.0.2.57,镜像版本号:OpenHarmony_5.0.2.58。
61
623.本示例需要使用DevEco Studio 5.0.1 Release (Build Version: 5.0.5.306, built on December 6, 2024)及以上版本才可编译运行。
63
64### 下载
65
66如需单独下载本工程,执行如下命令:
67
68````
69git init
70git config core.sparsecheckout true
71echo code/DocsSample/ArkTs/NodeAPI/NodeApiClassicUseCases/NodeAPIAsynchronousTask > .git/info/sparse-checkout
72git remote add origin https://gitee.com/openharmony/applications_app_samples.git
73git pull origin master
74````