mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-05-29 14:31:19 +00:00
Add TimeoutError and throw on p_timeout
This commit is contained in:
parent
d890bf9280
commit
027f94c867
@ -26,6 +26,15 @@ class TransportError extends Error {
|
|||||||
}
|
}
|
||||||
errors.TransportError = TransportError;
|
errors.TransportError = TransportError;
|
||||||
|
|
||||||
|
class TimeoutError extends Error {
|
||||||
|
constructor(message) {
|
||||||
|
super(message || "Timed out");
|
||||||
|
this.name = "TimeoutError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
errors.TimeoutError = TimeoutError;
|
||||||
|
|
||||||
|
|
||||||
/*---- Below here are errors copied from previous Dweb-Transport and not currently used */
|
/*---- Below here are errors copied from previous Dweb-Transport and not currently used */
|
||||||
/*
|
/*
|
||||||
class ObsoleteError extends Error {
|
class ObsoleteError extends Error {
|
||||||
|
4
utils.js
4
utils.js
@ -1,3 +1,4 @@
|
|||||||
|
const errors = require('./Errors');
|
||||||
|
|
||||||
utils = {}; //utility functions
|
utils = {}; //utility functions
|
||||||
|
|
||||||
@ -26,12 +27,13 @@ utils.p_timeout = function(promise, ms, errorstr) {
|
|||||||
promise: A promise we want to watch to completion
|
promise: A promise we want to watch to completion
|
||||||
ms: Time in milliseconds to allow it to run
|
ms: Time in milliseconds to allow it to run
|
||||||
errorstr: Error message in reject error
|
errorstr: Error message in reject error
|
||||||
|
throws: TimeoutError on timeout with message = errorstr
|
||||||
*/
|
*/
|
||||||
let timer = null;
|
let timer = null;
|
||||||
|
|
||||||
return Promise.race([
|
return Promise.race([
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
timer = setTimeout(reject, ms, errorstr || `Timed out in ${ms}ms`);
|
timer = setTimeout(reject, ms, new errors.TimeoutError(errorstr || `Timed out in ${ms}ms`));
|
||||||
}),
|
}),
|
||||||
promise.then((value) => {
|
promise.then((value) => {
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user