Last active
April 29, 2020 11:14
-
-
Save CodingItWrong/5454aa6ceb2ff199e3ed785ada508e5d to your computer and use it in GitHub Desktop.
Async JS Functions' Shared scope
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
// option 1 | |
() => { | |
return call1().then(response1 => { | |
return call2(response1.field); | |
}).then(response2 => { | |
console.log({ response1, response2 }); // response1 not in scope | |
}); | |
}; | |
// option 2 | |
() => { | |
return call1().then(response1 => { | |
return call2(response1.field).then(response2 => { | |
console.log({ response1, response2 }); // nested promises not great | |
}); | |
}); | |
}; | |
// option 3 | |
async () => { | |
const response1 = await call1(); | |
const response2 = await call2(response1.field); | |
console.log({ response1, response2 }); // both responses available | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment