1/* 2* Copyright (C) 2025 HiHope Open Source Organization. 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*/ 15export const stringPromise: Promise<string> = new Promise((resolve) => { 16 setTimeout(() => resolve('Hello from string Promise'), 100); 17}); 18 19export const numberPromise: Promise<number> = new Promise((resolve) => { 20 setTimeout(() => resolve(42), 100); 21}); 22 23interface Interface_1 { 24 name: string; 25 age: number; 26} 27 28export const objectPromise: Promise<Interface_1> = new Promise((resolve) => { 29 setTimeout(() => resolve({ name: 'Alice', age: 30 }), 100); 30}); 31 32export const arrayPromise: Promise<string[]> = new Promise((resolve) => { 33 setTimeout(() => resolve(['apple', 'banana', 'cherry']), 150); 34}); 35 36export const undefinedPromise: Promise<undefined> = new Promise((resolve) => { 37 setTimeout(() => resolve(undefined), 30); 38}); 39 40export const functionPromise: Promise<() => number> = new Promise((resolve) => { 41 setTimeout(() => resolve(() => 42), 120); 42}); 43 44export const nullPromise: Promise<null> = new Promise((resolve) => { 45 setTimeout(() => resolve(null), 50); 46}); 47 48export const errorPromise: Promise<never> = new Promise((_, reject) => { 49 setTimeout(() => reject(new Error('Standard error occurred')), 100); 50}); 51 52//resolve中使用上下文中的class对象/sendableClass对象/class(引用sendableClass)对象 53 54export class SendableClass { 55 public id: number; 56 public name: string; 57 public tags: string[]; 58 59 constructor( 60 id: number, 61 name: string, 62 tags: string[] = [] 63 ) { 64 this.id = id; 65 this.name = name; 66 this.tags = tags; 67 } 68 69 addTag(tag: string): void { 70 this.tags.push(tag); 71 } 72 73 serialize(): string { 74 return JSON.stringify({ 75 id: this.id, 76 name: this.name, 77 tags: this.tags 78 }); 79 } 80} 81 82export const existingSendablePromise = new Promise<SendableClass>((resolve) => { 83 setTimeout(() => { 84 const instance = new SendableClass(1, 'ExistingInstance', ['initial']); 85 resolve(instance); 86 }, 100); 87}); 88 89//resolve中抛出异常 90export function createProblematicPromise(): Promise<string> { 91 return new Promise((resolve, _) => { 92 resolve( 93 (() => { 94 throw new Error('Error thrown during resolution'); 95 })() 96 ); 97 }); 98} 99 100//resolve中使用setTimeout 101export function createDelayedPromise(value: number): Promise<number> { 102 return new Promise((resolve) => { 103 setTimeout(() => { 104 console.log(`Resolved with value: ${value}`); 105 resolve(value); 106 }, 200); 107 }); 108} 109 110export function createFaultyPromise(): Promise<string> { 111 return new Promise((_, reject) => { 112 setTimeout(() => { 113 try { 114 throw new Error('Something went wrong!'); 115 } catch (error) { 116 reject(error); 117 } 118 }, 100); 119 }); 120} 121 122//reject中使用上下文变量 123export function createFaultyPromise2(): Promise<string> { 124 return new Promise((_, reject) => { 125 const errorMessage = 'Something went wrong!'; 126 setTimeout(() => { 127 reject(new Error(errorMessage)); 128 }, 100); 129 }); 130} 131 132//reject中使用setTimeout 133export function createDelayedRejectPromise(): Promise<string> { 134 return new Promise((_, reject) => { 135 setTimeout(() => { 136 setTimeout(() => { 137 reject(new Error('Something went wrong!')); 138 }, 100); 139 }, 200); 140 }); 141} 142 143//导出Promise.all中使用setTimeout 144export function createPromiseAllWithContext(values: number[]): Promise<number[]> { 145 return new Promise((resolve, reject) => { 146 setTimeout(() => { 147 const promises = values.map((value) => createDelayedPromise(value)); 148 Promise.all(promises).then((result) => resolve(result)) 149 .catch((error: Error) => reject(error)) 150 .finally(() => { 151 console.info('end'); 152 }) 153 }, 200); 154 }); 155} 156 157 158export async function fetchData(input: string): Promise<string> { 159 return 'a'; 160} 161 162@Sendable 163export class SendableClass2 { 164 private value: string; 165 166 constructor(value: string) { 167 this.value = value; 168 } 169 170 getValue(): string { 171 return this.value; 172 } 173 174 setValue(value: string): void { 175 this.value = value; 176 } 177} 178 179@Sendable 180export function Sendablefun(num: number) { 181 return num 182} 183; 184