Last active
May 18, 2025 21:39
-
-
Save hartwork/fd7a5e2c635fa78a64ef3b4214b10a5c to your computer and use it in GitHub Desktop.
Demo waiting for a callback to finish in TypeScript
This file contains hidden or 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
#! /usr/bin/env node | |
// Copyright (c) 2025 Sebastian Pipping <[email protected]> | |
// SPDX-License-Identifier: 0BSD | |
function callCallbackSomeTime(callback: () => void): void { | |
setTimeout(function () { | |
callback(); | |
}, 2000); | |
} | |
function actualCallback() { | |
console.log("Inside callback"); | |
} | |
async function asyncMain() { | |
console.log("Before callback"); | |
await new Promise<void>((resolve) => { | |
callCallbackSomeTime(function () { | |
actualCallback(); | |
resolve(); | |
}); | |
console.log("Waiting for callback to finish..."); | |
}); | |
console.log("After callback"); | |
} | |
asyncMain(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment