Created
July 19, 2021 11:31
-
-
Save mfbx9da4/ea7ae7fbb3d5a681b71cd55087b2055e 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
const wait = () => new Promise((r) => setTimeout(r, 10)); | |
const fakeDbOp = async () => { | |
console.log("fakeDbOp"); | |
await wait(10); | |
return 1; | |
}; | |
const fakeTrxDbOp = async () => { | |
console.log("fakeTrxDbOp"); | |
await wait(10); | |
return 2; | |
}; | |
const f = () => { | |
let catchHandler; | |
let finallyHandler; | |
const then = (fn) => { | |
fakeDbOp() | |
.then((x) => fn(x)) | |
.catch((e) => { | |
if (catchHandler) catchHandler(e); | |
}) | |
.finally(() => { | |
if (finallyHandler) finallyHandler(e); | |
}); | |
}; | |
const trx = () => fakeTrxDbOp(); | |
return { | |
then, | |
catch: (fn) => { | |
catchHandler = fn; | |
}, | |
finally: (fn) => { | |
finallyHandler = fn; | |
}, | |
trx, | |
}; | |
}; | |
const main = async () => { | |
const a = await f(); | |
console.log("a", a); | |
const b = await f().trx(); | |
console.log("b", b); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment