Skip to content

Instantly share code, notes, and snippets.

@gapspt
Last active March 29, 2022 17:20
Show Gist options
  • Save gapspt/06d40e322ad96c9c43096e41b3e49046 to your computer and use it in GitHub Desktop.
Save gapspt/06d40e322ad96c9c43096e41b3e49046 to your computer and use it in GitHub Desktop.
JavaScript pending promises debug
// Adapted from https://makandracards.com/makandra/46681-javascript-how-to-query-the-state-of-a-promise
function promiseState(promise, callback) {
const uniqueValue = /unique/;
Promise.race([promise, Promise.resolve(uniqueValue)])
.then(
(value) => callback(value === uniqueValue ? 'pending' : 'fulfilled'),
(reason) => callback(`rejected (${reason})`));
}
function debugPromiseState(promise) {
promiseState(promise, (state) => {
console.log(state);
if (state === 'pending') {
setTimeout(() => debugPromiseState(promise), 1000);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment