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 16import { AsyncCallback, BusinessError, Callback, ErrorCallback } from '@ohos.base'; 17import hilog from '@ohos.hilog'; 18 19const SYNTAX_ERROR_CODE: double = 1002; 20 21export class MyCallback { 22 static myCallback(callback: Callback<string>): void { 23 hilog.info(0x0000, 'testTag', 'myCallback'); 24 callback('myCallback'); 25 } 26 27 static myAsyncCallback(callback: AsyncCallback<string, string>): void { 28 hilog.info(0x0000, 'testTag', 'myAsyncCallback'); 29 let error = new Error('Business Error', 'basicError2 message', undefined); 30 let be2: BusinessError<string> = new BusinessError<string>(SYNTAX_ERROR_CODE, error); 31 callback(be2, 'yAsyncCallback'); 32 } 33 34 static myErrorCallback(callback: ErrorCallback<BusinessError<string>>): void { 35 hilog.info(0x0000, 'testTag', 'myErrorCallback'); 36 let error = new Error('Business Error', 'myErrorCallback message', undefined); 37 let result: string = 'test'; 38 let be: BusinessError<string> = new BusinessError<string>(SYNTAX_ERROR_CODE, result, error); 39 callback(be); 40 } 41 42 static runCasesOfCallback() { 43 MyCallback.myCallback((data: string) => { 44 hilog.info(0x0000, 'testTag', 'MyCallback' + data); 45 }) 46 MyCallback.myAsyncCallback((error: BusinessError<string> | null, result: string | undefined) => { 47 hilog.info(0x0000, 'testTag', `myAsyncCallback, error: ${error?.message}, result:${result}`); 48 }) 49 MyCallback.myErrorCallback((error: BusinessError<string>) => { 50 hilog.info(0x0000, 'testTag', `myAsyncCallback, error: ${error.message}`); 51 }) 52 } 53}