Last active
March 29, 2022 17:20
-
-
Save gapspt/06d40e322ad96c9c43096e41b3e49046 to your computer and use it in GitHub Desktop.
JavaScript pending promises debug
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
// 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