Add TimeoutError and throw on p_timeout

This commit is contained in:
Mitra Ardron 2018-05-08 16:46:34 -07:00
parent d890bf9280
commit 027f94c867
2 changed files with 12 additions and 1 deletions

View File

@ -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 {

View File

@ -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);