ecmascript-next

async/await implicitly returns promise?

帅比萌擦擦* 提交于 2019-12-27 10:20:12
问题 I read that async functions marked by the async keyword implicitly return a promise: async function getVal(){ return await doSomethingAync(); } var ret = getVal(); console.log(ret); but that is not coherent...assuming doSomethingAsync() returns a promise, and the await keyword will return the value from the promise, not the promise itsef, then my getVal function should return that value, not an implicit promise. So what exactly is the case? Do functions marked by the async keyword implicitly

How to use the proposed ECMAscript class syntax with React and Webpack?

☆樱花仙子☆ 提交于 2019-12-25 08:56:58
问题 I am trying to learn the proposed class syntax for ecmascript and using it with React, i have successfully rendered components with es6 using babel with webpack. Now i want to use instance properties inside classes which are declared outside of the constructor. For eg: class MyComponent extends React.Component{ constructor(props){ super(props) } property1= "new property"; func1= ()=>{ } } I am getting the error "unexpected token" on 'property1' and 'func1' while trying to do something like

How to enable support for class properties in the latest version BabelJS?

為{幸葍}努か 提交于 2019-12-25 05:55:12
问题 Does anyone know how to enable support for properties of a class in the latest version BabelJS? import React from 'react'; import {Component} from 'react'; export default class Button extends Component { constructor(){ super(); } myProp = {}; // ERROR: /path/to/file/Button.jsx: Unexpected token (9:11) render(){ return <div></div>; } } 回答1: With Babel 6, you use the syntax-class-properties plugin, by installing it: npm install babel-plugin-syntax-class-properties and adding it to your .babelrc

How to enable support for class properties in the latest version BabelJS?

蹲街弑〆低调 提交于 2019-12-25 05:55:03
问题 Does anyone know how to enable support for properties of a class in the latest version BabelJS? import React from 'react'; import {Component} from 'react'; export default class Button extends Component { constructor(){ super(); } myProp = {}; // ERROR: /path/to/file/Button.jsx: Unexpected token (9:11) render(){ return <div></div>; } } 回答1: With Babel 6, you use the syntax-class-properties plugin, by installing it: npm install babel-plugin-syntax-class-properties and adding it to your .babelrc

Use object spread operator to construct function object with fields

我的梦境 提交于 2019-12-24 01:26:05
问题 I have successfully created a function object with additional fields using the following Object.assign : Object.assign((a) => { // Do something.... }, { x: 10 }) ... by following a discussion on typescript interfaces: Implementing TypeScript interface with bare function signature plus other fields However, I find the Object.assign syntax hard to read , is there an object spread operator way of doing this? Simply putting the function and object in curly brackets with two spreads does not seem

Constructor code not reachable

帅比萌擦擦* 提交于 2019-12-23 22:08:56
问题 I am passing a class method as a parameter to a new class instantiation like this: class Abc { constructor() { this.a = () => { }; } b = new Def(this.a); } I get 'cannot read property a of undefined' in browser console. Why is a undefined inside b = new Def(this.a) ? On debugging, I found that browser throws the error and the constructor code is never reached. Why is this happening? Note: I am using babel, so I can use class fields and hence b = new Def() is a valid syntax here. 回答1: That's

Abort ecmascript7 async function

荒凉一梦 提交于 2019-12-23 12:34:05
问题 Is there a way to cancel a ES7 async function? In this example, on click, I want to abort async function call before calling new. async function draw(){ for(;;){ drawRandomRectOnCanvas(); await sleep(100); } } function sleep(t){ return new Promise(cb=>setTimeout(cb,t)); } let asyncCall; window.addEventListener('click', function(){ if(asyncCall) asyncCall.abort(); // this dont works clearCanvas(); asyncCall = draw(); }); 回答1: There's nothing built in to JavaScript yet, but you could easily

Override constructor with an class decorator?

醉酒当歌 提交于 2019-12-23 12:23:06
问题 How can I override a constructor with an ES7 class decorator? For example, I'd like to have something like: @injectAttributes({ foo: 42 }) class Bar { constructor() { console.log(this.foo); } } Where the injectAttributes decorator will inject attributes into new instances before they are created: > bar = new Bar(); 42 > bar.foo 42 The obvious solution – using a different constructor: function overrideConstructor(cls, attrs) { Object.assign(this, attrs); cls.call(this); } Does not work because

Arrow functions as class properties using Babel

こ雲淡風輕ζ 提交于 2019-12-23 07:49:55
问题 Can someone explain how Babel in React supports fat arrow functions as class properties? Using Babel Try it out I can see they are not supported: class Question { // Property (not supported) myProp = () => { return 'Hello, world!'; } // Method (supported) myFunc() { return 'Hello, world!'; } } Class properties are not supported in ES6 (correct me if I'm wrong) but then in React (with Babel) they work. I can see the difference between methods and properties using TypeScript Playground but I

Succinct/concise syntax for 'optional' object keys in ES6/ES7?

泪湿孤枕 提交于 2019-12-20 11:03:16
问题 There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript: const obj = { requiredKey1: ..., requiredKey2: ... }; if (someCondition) { obj.optionalKey1 = ...; } Is there a way to define the object all at once with both optional and required keys? 回答1: You can use object spread to have an optional property. Note: Object Rest/Spread is a stage 4 proposal for ECMAScript. You might need the babel transform to use it.