Skip to content

Instantly share code, notes, and snippets.

@davidraedev
Last active July 25, 2017 08:44
Show Gist options
  • Save davidraedev/c9a0aedb33dda19a992b42d07e590451 to your computer and use it in GitHub Desktop.
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
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