to函数 对try catch 的函数包装,解决地狱嵌套,使函数扁平化,函数单一化原则
to函数 对try catch 的函数包装,解决地狱嵌套,使函数扁平化,函数单一化原则
// utils/promise.js
export function to(promise) {
return promise
.then(data => [null,data])
.catch(err =>[err, undefined]);
}

import { fetchUserById } from './api';
import { to }from'./utils/promise';
async function displayUser(userId) {
const [error,user] = await to(fetchUserById(userId));
if (error || !user) {
console.error('获取用户失败:,error);
//..相应的错误处理逻辑
return;
}
// 到这里,代码的”快乐路径"是清晰且扁平的
console.log('用户信息:,user.name);
// ... 更多基于 user 的操作

ref
别再手动 try...catch 了:一种更优雅的 async/await 错误处理模式
https://mp.weixin.qq.com/s/iBK0AUavQeRzOvKBbm7uCw
---------------------------------------------
生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
https://pengchenggang.gitee.io/navigator/
SMART原则:
目标必须是具体的(Specific)
目标必须是可以衡量的(Measurable)
目标必须是可以达到的(Attainable)
目标必须和其他目标具有相关性(Relevant)
目标必须具有明确的截止期限(Time-based)
生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
https://pengchenggang.gitee.io/navigator/
SMART原则:
目标必须是具体的(Specific)
目标必须是可以衡量的(Measurable)
目标必须是可以达到的(Attainable)
目标必须和其他目标具有相关性(Relevant)
目标必须具有明确的截止期限(Time-based)

浙公网安备 33010602011771号