How JavaScript Promise Works
Check out the article, the code and the commit history.
Tests use asyncCounter, perhaps another good read.
asyncCounter