Skip to content

Instantly share code, notes, and snippets.

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 netpoetica/9d7d4a551d317dc28afc to your computer and use it in GitHub Desktop.
Save netpoetica/9d7d4a551d317dc28afc to your computer and use it in GitHub Desktop.
An Example of Why Promises and Generators are Important
// An Example of Why Promises and Generators are Important
// Based on this awesome video: https://www.youtube.com/watch?v=hf1T_AONQJU
// You can just copy and paste this code into your console to see it in action
var App = function App(){
// Named anonymous functions for clear picture
// of the call stack in the error stack trace
this.asyncError = function myAsynchronousError(){
throw new Error('Asynchronous error');
};
this.syncError = function mySynchronousError(){
throw new Error('Synchronous error');
};
// Capitlized instead of camelCased to keep the name clear
this.Return = function Return(){
return arguments.callee;
};
};
(function global(){
var app = new App();
// Synchronous Return
// Able to access the returned value
// of app.Return which is app.Return.toString()
console.log(app.Return());
// Asynchronous Return
// The returned value cannot be accessed. The value of 1 is printed
// when setTimeout is succesfully called.
console.log(setTimeout(app.Return, 1000));
// Asynchronous Error
// The callstack does not have the context of
// the App instance, the global anonymous
// boot function, or the anonymous JS function
// at the root of the program.
/*
Uncaught Error: Asynchronous error index.html:7
myAsynchronousError
*/
setTimeout(app.asyncError, 1000);
// Synchronous Error
// The throw statement gives you the entire call stack:
/*
Uncaught Error: Synchronous error index.html:11
mySynchronousError index.html:11
global index.html:27
(anonymous function)
*/
app.syncError();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment