diff --git a/asynctimeout.js b/asynctimeout.js index bfd5df1..401e42b 100644 --- a/asynctimeout.js +++ b/asynctimeout.js @@ -15,10 +15,21 @@ /// See `examples` below. function AsyncTimeout(thing, interval) { + function isPromise(obj) { + return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'; + } + + async function _call(t) + { + if(isPromise(t)) return await t(arguments); + else await (async () => {}); + return t(arguments); + } + this.timeout = () => new Promise((resolve, reject) => { setTimeout(() => { try { - resolve(thing()); + _call(thing).then(resolve); //XXX: This design doesn't work, we'll need to go back to the drawing board with this function for this to work... The exception won't propagate here, so... } catch(e) { reject(e); } }, interval); }); @@ -26,7 +37,7 @@ function AsyncTimeout(thing, interval) while(true) { yield await new Promise((resolve, reject) => { setTimeout(() => { - try { resolve(thing()); } catch(e) { reject(e); } + try { _call(thing).then(resolve); } catch(e) { reject(e); } }, interval); }); }