Skip to content

Instantly share code, notes, and snippets.

@vasanthk
Last active December 3, 2015 08:02
Show Gist options
  • Select an option

  • Save vasanthk/dc4cb612b089085d7b74 to your computer and use it in GitHub Desktop.

Select an option

Save vasanthk/dc4cb612b089085d7b74 to your computer and use it in GitHub Desktop.
For understanding callback hell better... and inversion of control.
var cbFn = function() {
console.log('Done')
}
// Run Async API call
completeAsyncCheckout(data, cbFn);
cbFn = function() {
// Guessing it prints 'Finished' since it has reference.
console.log('Finished');
}
// In this scenario:
// Have this qn because from your talk - 'Syncing Async' you mentioned that due to inversion of control they could potentially call the cbFn multiple times.
// Wondering how that could happen?
// The callback is called once the request completes right? Or is there a way a callback can be triggered multiple times once a request is complete?
// What controls how many times the success callback will be called?
@vasanthk
Copy link
Author

vasanthk commented Dec 3, 2015

@getify Did a setTimeout(cbFn, 3000);
It makes sense that setTimeout is a web api and it ends up calling the updated fn after 3000ms.
Will such a thing happen if I passed a cbFn to an external async API request via completeAsyncCheckout (which takes a fews secs as well)?

@getify
Copy link

getify commented Dec 3, 2015

no, because once you pass the reference to your orig function to somewhere elsr, if you then re-assign your reference to a new function, the passed reference still refers to the orig function.

@vasanthk
Copy link
Author

vasanthk commented Dec 3, 2015

@getify Gotcha.. Thanks a lot. You're my JS hero!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment