Skip to content

Instantly share code, notes, and snippets.

@hartwork
Last active May 18, 2025 21:39
Show Gist options
  • Save hartwork/fd7a5e2c635fa78a64ef3b4214b10a5c to your computer and use it in GitHub Desktop.
Save hartwork/fd7a5e2c635fa78a64ef3b4214b10a5c to your computer and use it in GitHub Desktop.
Demo waiting for a callback to finish in TypeScript
#! /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