Skip to content

Instantly share code, notes, and snippets.

@CodingItWrong
Last active April 29, 2020 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodingItWrong/5454aa6ceb2ff199e3ed785ada508e5d to your computer and use it in GitHub Desktop.
Save CodingItWrong/5454aa6ceb2ff199e3ed785ada508e5d to your computer and use it in GitHub Desktop.
Async JS Functions' Shared scope
// 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