Skip to content

Instantly share code, notes, and snippets.

@appellation
Last active January 6, 2017 17:14
Show Gist options
  • Save appellation/89eaaac33d092ef321506d7b15d4af54 to your computer and use it in GitHub Desktop.
Save appellation/89eaaac33d092ef321506d7b15d4af54 to your computer and use it in GitHub Desktop.
A quick promise chaining demonstration.
// not chaining
asyncFunction().then(data => {
asyncFunction(data).then(data2 => {
asyncFunction(data2);
}).catch(console.error);
});
// chaining
asyncFunction.then(data => {
return asyncFunction(data);
}).catch(console.error).then(data2 => { // this catch statement would catch all previously chained promises
return asyncFunction(data2);
});
function asyncFunction(say) {
return new Promise(resolve => {
setTimeout(() => {
resolve(say || 'async complete');
}, 1000);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment