Last active
August 29, 2015 13:57
-
-
Save WesSouza/9771039 to your computer and use it in GitHub Desktop.
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
function checkUrl ( url ) { | |
function checkUrlInternal ( resolve, reject, url, count ) { | |
count = count || 0; | |
posts.find( { url: url } ) | |
.then( function ( posts ) { | |
if ( posts.length ) { | |
count ++; | |
if ( count > 10 ) { | |
reject( new Error( 'url:tooManyReplaces' ) ); | |
} | |
url = url.replace( /-\d+$/, '' ) +'-'+ count; | |
return checkUrlInternal( resolve, reject, url, count ); | |
} | |
else { | |
resolve( url ); | |
} | |
} ) | |
.catch( function ( err ) { | |
reject( err ) | |
} ); | |
} | |
return new Promise( function ( resolve, reject ) { | |
checkUrlInternal( resolve, reject, url ); | |
} ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dá pra fazer a recursão simplesmente retornando uma nova promise:
Mas a versão do @cranic faz a mesma coisa sem a complicação conceitual toda :)