mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-03-14 18:10:49 +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;
|
||||
|
||||
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 */
|
||||
/*
|
||||
class ObsoleteError extends Error {
|
||||
|
4
utils.js
4
utils.js
@ -1,3 +1,4 @@
|
||||
const errors = require('./Errors');
|
||||
|
||||
utils = {}; //utility functions
|
||||
|
||||
@ -26,12 +27,13 @@ utils.p_timeout = function(promise, ms, errorstr) {
|
||||
promise: A promise we want to watch to completion
|
||||
ms: Time in milliseconds to allow it to run
|
||||
errorstr: Error message in reject error
|
||||
throws: TimeoutError on timeout with message = errorstr
|
||||
*/
|
||||
let timer = null;
|
||||
|
||||
return Promise.race([
|
||||
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) => {
|
||||
clearTimeout(timer);
|
||||
|
Loading…
x
Reference in New Issue
Block a user