Skip to content

Instantly share code, notes, and snippets.

@cakoose
Created July 27, 2021 00:28
Show Gist options
  • Save cakoose/131510fa5675e27ac188b23c94a85901 to your computer and use it in GitHub Desktop.
Save cakoose/131510fa5675e27ac188b23c94a85901 to your computer and use it in GitHub Desktop.
"use strict";
function sleep() { return new Promise(resolve => setTimeout(resolve, 1)); }
class Query {
constructor(f) {
this.f = f;
}
then(onFulfilled, onRejected) {
this.f()
.then(r => onFulfilled ? onFulfilled(r) : r)
.catch(onRejected);
}
async exec() {
await this.f();
}
}
async function f2() {
await sleep();
// Just await
await new Query(async () => { throw new Error('blah'); });
// Call exec()
await new Query(async () => { throw new Error('blah'); }).exec();
}
async function f1() {
await sleep();
await f2();
}
async function mainAsync() {
try {
await f1();
}
catch (err) {
console.log('err', err);
}
}
if (require.main === module) {
mainAsync().catch(err => {
console.error(err);
process.exit(1);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment