审判
"我向全能的上帝保证,我将提供的证据 是真相,整个真相,没有 但事实并非如此
一个伟大的工具,确保您的代码遵守它的承诺!
如何安装
使用您最喜欢的包管理器 :
pnpm i @coniface/trial
yarn add @coniface/trial
npm i @coniface/trial
如何用取来?
再简单不过了!
- 减少承诺声明缩进
// Instead of
const promise = new Promise((resolve, reject) => {
console.log(`Let's get to work!`);
});
import { getTrial } from '@coniface/trial';
// Inline your promise
const [promise, resolve, reject] = getTrial<T>();
console.log(`Let's get to work!`);
i 自2024年初起,成为承诺API的一部分
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global Objects/Promise/ with Resolvers 互联网档案馆的存檔,存档日期2013-12-22
- 鼓励使用强打字而不是尝试/捕获区块
// Instead of
async function somewhereInYourCode<T>(promise: Promise<T>) {
try {
const result = await promise;
console.log(`Let's get to work!`);
} catch (error) {
console.error('An error occured', error);
}
}
import { trial } from '@coniface/trial';
// Use typings and inline your code
async function somewhereInYourCode<T>(promise: Promise<T>) {
const [data, error] = await trial(promise);
if (error) {
// error exists in this context
// data type is never
console.error('An error occured', error);
return;
}
// error does not exist in this context
// data type is T
console.log(`Let's get to work!`);
}
为什么这么命名?
trial是一个可用作函数的简明的5个字母词.- 提及法庭上的承诺.
- 与承诺状态相匹配的结果(有罪/无罪)(已解决/驳回).