@actualwave/resolve-or-timeout 中文文档教程
@actualwave/resolve-or-timeout
在 Promise.race() 中运行的函数是您的承诺和超时,如果超时先完成,则结果承诺会因错误而拒绝。
export const resolveOrTimeout = <T = unknown>(
// Promise or a function that will be passed to a Promise object
promiseOrHandler:
| Promise<T>
| ((
resolve: (data: T) => void,
reject?: (data: unknown) => void
) => unknown),
// timeout in milliseconds. if 0, race condition will not apply and original promise will be returned as is
timeout: number,
// optional, timeout error message
timeoutError?: string,
// optional callback that will be executed when timeout completes first
onTimeout?: (msg: string) => void
) => Promise<T>;
Example
try {
/*
if request resolves in less than 500 ms, you will get response
*/
const response = await resolveOrTimeout(
fetch('/super/long/request'),
500,
'Sorry, your request takes toooooo long.',
);
} catch (error) {
/*
if request takes longer than 500 ms, promise rejects with error message
*/
}
它接受承诺或将传递给承诺的函数。
const response = await resolveOrTimeout(
(resolve) => {
// do something then resolve
},
500
);
@actualwave/resolve-or-timeout
Function that runs in Promise.race() your promise and a timeout, if timeout completes first, resulting promise rejects with error.
export const resolveOrTimeout = <T = unknown>(
// Promise or a function that will be passed to a Promise object
promiseOrHandler:
| Promise<T>
| ((
resolve: (data: T) => void,
reject?: (data: unknown) => void
) => unknown),
// timeout in milliseconds. if 0, race condition will not apply and original promise will be returned as is
timeout: number,
// optional, timeout error message
timeoutError?: string,
// optional callback that will be executed when timeout completes first
onTimeout?: (msg: string) => void
) => Promise<T>;
Example
try {
/*
if request resolves in less than 500 ms, you will get response
*/
const response = await resolveOrTimeout(
fetch('/super/long/request'),
500,
'Sorry, your request takes toooooo long.',
);
} catch (error) {
/*
if request takes longer than 500 ms, promise rejects with error message
*/
}
It accepts promise or a function that will be passed to a promise.
const response = await resolveOrTimeout(
(resolve) => {
// do something then resolve
},
500
);
更多
你可能也喜欢
- @0negativ/hawtio-integration 中文文档教程
- @0x04/string-mutilator 中文文档教程
- @11ways/exiv2 中文文档教程
- @17media/eslint-config-17media 中文文档教程
- @2012mjm/telegram-tl-node 中文文档教程
- @3wirebuild/styled-system 中文文档教程
- @42.nl/spring-connect 中文文档教程
- @4geit/swg-chance-set-false-helper 中文文档教程
- @4tunaio/4tuna-iot-node-red 中文文档教程
- @5no/i18n 中文文档教程