export async function fetchUser(): Promise<User> { const data = await fetch('https://www.example.com/user').then(res => { return res.json() }) return data } // 我们开发中在获取到了User类型 type UserFetch = Awaited<ReturnType<typeof fetchUser>> async function getUserInfo() { let ...
function hRequest<T = any>(p: Promise<any>): [T, any] { return p.then(res => [res, undefined]).catch(err => [undefined, err]) as unknown as [T, any]; } export async function Get<T = any>(p: { url: string, params: any }): Promise<[T,any]>{ const { url, params }...
export async function ApiHello(call: ApiCall<ReqHello, ResHello>) { call.succ({ reply: 'Hello, ' + call.req.name }); } 客户端调用 let ret = await client.callApi('Hello', { name: 'World' }); console.log(ret); // { isSucc: true, res: { reply: 'Hello, World' } } 全...
import{IAccount,ILoginResult,IDataType}from'./type'importxwlRequestsfrom'../index'enumLoginApi{AccountLogin='/login',LoginUserInfo='/users/',// 用法: /users/1UserMenus='/role/'// 用法: role/1/menu}exportfunctionaccountLoginRequest(account:IAccount){returnxwlRequests.post<IDataType<ILoginRes...
有些语法的使用是有要求的,比如 await 只在 async 函数内部才作关键字。 源码中用闭包内全局的变量存储这些信息。思路是:先设置允许 await 标记位,然后解析表达式(这时标记位已设置成允许 await),解析完成则清除标记位。 functiondoInAwaitContext<T>(func: () =>T): T {returndoInsideOfContext(NodeFlags.Awai...
export async function ApiAddPost(call: ApiCall<ReqAddPost, ResAddPost>) { if(call.req.newpost.name){ call.success({ msg: "hello," + call.req.newpost.name }) }else{ call.error('Invalid name'); } } 我们的第一个api已经写完了,我们需要正常的过一次test,然后我们在让前端去调用。
asyncfunctionrenderWidget(){constcontainer=document.getElementById("widget");if(container!==null){constwidget=awaitimport("./widget");widget.render(container);}}renderWidget(); 清晰明了,不要忘记通过在其声明中添加async关键字来使renderWidget函数异步化。
// say.tsexport function hi() { console.log('Hello');}export function bye() { console.log('Bye');} 那么,可以像下面这样进行动态导入: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 asyncfunctiontest(){letns=awaitimport('./say');lethi=ns.hi;letbye=ns.bye;hi();bye();} ...
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库:https://github.com/k8w/tsrpc master 2.3.2 2.2.19 2.3.1 2.2.18 2.2.17 克隆/下载 克隆/下载 HTTPSSSHSVNSVN+SSH下载ZIP 该操作需登录 Gitee 帐号,请先登录后再操作。 立即登录没有帐号,去注册 ...
namespace Math{exportfunctionadd(x,y){...}} 2. null 和 undefined 是其它类型(包括 void)的子类型,可以赋值给其它类型(如:数字类型),赋值后的类型会变成 null 或 undefined 默认情况下,编译器会提示错误,这是因为 tsconfig.json 里面有一个配置项是默认开启的。