Skip to content

Instantly share code, notes, and snippets.

@branneman
Last active September 20, 2016 16:34
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 branneman/ca4eaa8fc6e4bb3a20b7b8fc680ad8e0 to your computer and use it in GitHub Desktop.
Save branneman/ca4eaa8fc6e4bb3a20b7b8fc680ad8e0 to your computer and use it in GitHub Desktop.
ES2015: Returning a Promise from a constructor — Warning: I'm fairly sure this is always an anti-pattern.
class Parent {
constructor() {
return new Promise(resolve => {
setTimeout(() => resolve({ data: 'important' }), 1e3);
});
}
}
class Child extends Parent {
constructor() {
return super().then(console.log);
}
}
( new Parent() ).then(console.log);
//=> { data: 'important' }
( new Child() );
//=> { data: 'important' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment