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.

Fortune for async-timeout-js's current commit: Middle blessing − 中吉
allow-await-async-timeout-func
Avril 3 years ago
parent 66a5c7cc05
commit f9f85f36e1
Signed by: flanchan
GPG Key ID: 284488987C31F630

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

Loading…
Cancel
Save