This is my code:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function user() {
await mylibwrapper(async () => {
await sleep(1110); // take long time to done
console.log("fn");
})
// This out put must be after fn. How?
console.log("user");
}
async function mylibwrapper(fn) {
// We can wrap or mock fb before pass to mainlib
await mainlib(fn);
// How to wait until fn be called and finished? Then we can return and let the caller continue
console.log("mylibwrapper");
}
async function mainlib(fn) {
await sublib(fn);
}
async function sublib(fn) {
fn();
}
user();
I'm wrapping a library for my user. How can I force mylibwrapper
to wait until the callback fn
has finished before return the result to the user?
The output:
mylibwrapper
user
fn
The expected result in console output is "fn" before "user". Can you help me?
Conditions: We cannot change the code by user or libs (mainlib, sublib). We just can change the code in mylibwrapper
or wrap/mock fn
before passing to mainlib
.