异步接口请求中获取返回值
2025年1月19日小于 1 分钟
异步接口请求中获取返回值
详情
// 让本文件被定义为一个模块 允许使用顶层await
export {};
/** 等待函数 固定耗时 以便模拟异步接口请求 */
function function wait(): Promise<unknown>
等待函数 固定耗时 以便模拟异步接口请求wait() {
return new var Promise: PromiseConstructor
new <unknown>(executor: (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void) => Promise<unknown>
Creates a new Promise.Promise((resolve: (value: unknown) => void
resolve) => {
function setTimeout<[]>(callback: () => void, delay?: number): NodeJS.Timeout (+2 overloads)
Schedules execution of a one-time `callback` after `delay` milliseconds.
The `callback` will likely not be invoked in precisely `delay` milliseconds.
Node.js makes no guarantees about the exact timing of when callbacks will fire,
nor of their ordering. The callback will be called as close as possible to the
time specified.
When `delay` is larger than `2147483647` or less than `1`, the `delay` will be set to `1`. Non-integer delays are truncated to an integer.
If `callback` is not a function, a `TypeError` will be thrown.
This method has a custom variant for promises that is available using `timersPromises.setTimeout()`.setTimeout(() => {
resolve: (value: unknown) => void
resolve(void 0);
}, 1000);
});
}
/** 一个异步函数 */
function function asyncFun(query?: string): Promise<{
data: {
info: string;
};
msg: string;
code: number;
}>
一个异步函数asyncFun(query: string
query: string = "你是笨猪,哈哈哈哈") {
const const response: {
data: {
info: string;
};
msg: string;
code: number;
}
response = {
data: {
info: string;
}
data: {
info: string
info: query: string
query,
},
msg: string
msg: "success",
code: number
code: 200,
};
type type Response = {
data: {
info: string;
};
msg: string;
code: number;
}
Response = typeof const response: {
data: {
info: string;
};
msg: string;
code: number;
}
response;
return new var Promise: PromiseConstructor
new <{
data: {
info: string;
};
msg: string;
code: number;
}>(executor: (resolve: (value: {
data: {
info: string;
};
msg: string;
code: number;
} | PromiseLike<{
data: {
info: string;
};
msg: string;
code: number;
}>) => void, reject: (reason?: any) => void) => void) => Promise<...>
Creates a new Promise.Promise<type Response = {
data: {
info: string;
};
msg: string;
code: number;
}
Response>(async (resolve: (value: {
data: {
info: string;
};
msg: string;
code: number;
} | PromiseLike<{
data: {
info: string;
};
msg: string;
code: number;
}>) => void
resolve) => {
await function wait(): Promise<unknown>
等待函数 固定耗时 以便模拟异步接口请求wait();
resolve: (value: {
data: {
info: string;
};
msg: string;
code: number;
} | PromiseLike<{
data: {
info: string;
};
msg: string;
code: number;
}>) => void
resolve(const response: {
data: {
info: string;
};
msg: string;
code: number;
}
response);
});
}
/** 一个具体的对象 */
// @ts-ignore
const const formState: {
info: string;
}
一个具体的对象formState = await function asyncFun(query?: string): Promise<{
data: {
info: string;
};
msg: string;
code: number;
}>
一个异步函数asyncFun().Promise<{ data: { info: string; }; msg: string; code: number; }>.then<{
info: string;
}, never>(onfulfilled?: ((value: {
data: {
info: string;
};
msg: string;
code: number;
}) => {
info: string;
} | PromiseLike<{
info: string;
}>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<...>) | ... 1 more ... | undefined): Promise<...>
Attaches callbacks for the resolution and/or rejection of the Promise.then((res: {
data: {
info: string;
};
msg: string;
code: number;
}
res) => {
return res: {
data: {
info: string;
};
msg: string;
code: number;
}
res.data: {
info: string;
}
data;
});
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.log(" formState ", const formState: {
info: string;
}
一个具体的对象formState);