• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Single I/O Task Development
2
3
4Asynchronous concurrency provided by Promise and async/await is applicable to the development of a single I/O task. The following uses the asynchronous concurrency capability to write a file as an example.
5
6
71. Implement the logic of a single I/O task.
8
9   ```js
10   import fs from '@ohos.file.fs';
11
12   async function write(data: string, filePath: string) {
13     let file = await fs.open(filePath, fs.OpenMode.READ_WRITE);
14     fs.write(file.fd, data).then((writeLen) => {
15       fs.close(file);
16     }).catch((err) => {
17       console.error(`Failed to write data. Code is ${err.code}, message is ${err.message}`);
18     })
19   }
20   ```
21
222. Use the asynchronous capability to invoke the single I/O task. For details about how to obtain **filePath** in the example, see [Obtaining Application File Paths](../application-models/application-context-stage.md#obtaining-application-file-paths).
23
24   ```js
25   let filePath = ...; // Application file path
26   write('Hello World!', filePath).then(() => {
27     console.info('Succeeded in writing data.');
28   })
29   ```
30