Created
July 27, 2021 00:28
-
-
Save cakoose/131510fa5675e27ac188b23c94a85901 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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