import type { Result } from '../../mod.js'; /** ## wait : Convert asynchronous functions into synchronous execution 模拟[代数效应](https://mongkii.com/blog/2021-05-08-talk-about-algebraic-effects)在JS引擎的实现. 配合`run_effect`函数能实现 将异步代码转化为同步执行 的效果 @example ```typescript const func1 = async () => {//mock async incident const pro1: string = await new Promise(res => { setTimeout(() => { res('Yes')}, 3000) }) return pro1 } async function func2(){ const pro2: string = await new Promise(res => setTimeout(() => {res('No') }, 2000) ) return pro2 } const running =()=> { const A=wait(func1) console.log(A.unwrap()) // 'Yes' const B=wait(func2) console.log(B.unwrap())//'No' return A.unwrap()+B.unwrap() } run_effect( running, (res)=>{ console.log(res.unwrap())//'YesNo' }) ``` ### 注意事项⚠️ + 除了`wait()`内的函数,`run_effect()`内执行过程必须是[**纯函数**](https://zh.wikipedia.org/zh-hant/%E7%BA%AF%E5%87%BD%E6%95%B0) + `wait()`需要包裹在`run_effect()`内执行,`run_effect()`对于外部来说是一个异步函数,后续操作要在回调中执行 + `wait(fn)`传入的fn不能是个匿名函数,必须是个**具名函数**,否则会报错 + `wait()`语句尽量放在执行过程前面,能提高执行效率(由于JS并不支持代数效应导致) @category Function */ export declare const wait: (fn: () => Promise) => Result; /** `wait`函数的运行环境 */ export declare function run_effect>(fn: () => T, callback?: (res: T) => any): void; export declare function run_effect(fn: () => T, callback?: (res: Result) => any): void; //# sourceMappingURL=algebraicEffect.d.ts.map