1# Single I/O Task Development (Promise and Async/Await) 2 3 4Asynchronous concurrency provided by Promise and async/await is applicable to the development of a single I/O task. The following example uses the asynchronous concurrency capability to write data to a file. 5 6 71. Implement the logic of a single I/O task. 8 9 ```ts 10 import { fileIo } from '@kit.CoreFileKit' 11 import { BusinessError } from '@kit.BasicServicesKit'; 12 import { common } from '@kit.AbilityKit' 13 14 async function write(data: string, file: fileIo.File): Promise<void> { 15 fileIo.write(file.fd, data).then((writeLen: number) => { 16 console.info('write data length is: ' + writeLen) 17 }).catch((err: BusinessError) => { 18 console.error(`Failed to write data. Code is ${err.code}, message is ${err.message}`); 19 }) 20 } 21 ``` 222. Use the asynchronous concurrency 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 ```ts 25 async function testFunc(): Promise<boolean> { 26 let context = getContext() as common.UIAbilityContext; 27 let filePath: string = context.filesDir + "/test.txt"; // Application file path 28 let file: fileIo.File = await fileIo.open(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 29 write('Hello World!', file).then(() => { 30 console.info('Succeeded in writing data.'); 31 fileIo.close(file); 32 }).catch((err: BusinessError) => { 33 console.error(`Failed to write data. Code is ${err.code}, message is ${err.message}`); 34 fileIo.close(file); 35 }) 36 37 let result = await fileIo.access(filePath, fileIo.AccessModeType.EXIST); 38 if (!result) { 39 return false; 40 } 41 return true; 42 } 43 44 @Entry 45 @Component 46 struct Index { 47 @State message: string = 'Hello World'; 48 build() { 49 Row() { 50 Column() { 51 Text(this.message) 52 .fontSize(50) 53 .fontWeight(FontWeight.Bold) 54 .onClick(async () => { 55 let res = await testFunc(); 56 console.info("res is: " + res); 57 }) 58 } 59 .width('100%') 60 } 61 .height('100%') 62 } 63 } 64 ``` 65