From f9f85f36e118ff66ef96179437802ec19860408b Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 11 Feb 2022 21:07:48 +0000 Subject: [PATCH] Failed attempt at allowing async functions to be passed as `thing`: This will need a complete redesign to support this and I don"t care enough right now to do it. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for async-timeout-js's current commit: Middle blessing − 中吉 --- asynctimeout.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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); }); }