Last active
July 25, 2017 08:44
-
-
Save davidraedev/c9a0aedb33dda19a992b42d07e590451 to your computer and use it in GitHub Desktop.
promises with resolve inside a loop will resolve, but the loop will continue
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
function a() { | |
return new Promise( ( resolve, reject ) => { | |
let arr = [ 0,1,2,3,4,5 ]; | |
let stop = false; // short circuit the loop | |
arr.forEach(( num ) => { | |
if ( stop ) | |
return; | |
if ( num === 2 ) { | |
resolve( num ); | |
stop = true; | |
} | |
console.log( num ); | |
}); | |
}); | |
} | |
a().then( ( val ) => { | |
console.log( "resolve = ", val ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment