ecmascript-7

Ability to abort asynchronous call

十年热恋 提交于 2019-12-20 20:28:52
问题 I'm using babeljs with es7 style async/await methods. I have a main script that will call a async method on an array of objects that all return promises. I use Promise.all() to wait for all of those to return, however, these tasks could take a long time and if they exceed a threshold I would like to abort all of them, and the task handle that in an appropriate way. Is there anyway to accomplish such a thing? Currently the only way that I can think of is by spawning a process that does the

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.

Can you use es6 import alias syntax for React Components?

旧城冷巷雨未停 提交于 2019-12-20 09:12:36
问题 I'm trying to do something like the following, however it returns null: import { Button as styledButton } from 'component-library' then attempting to render it as: import React, { PropTypes } from "react"; import cx from 'classNames'; import { Button as styledButton } from 'component-library'; export default class Button extends React.Component { constructor(props){ super(props) } render() { return ( <styledButton {...this.props}></styledButton> ) } } The reason is, I need to import the

Why is Math.pow() (sometimes) not equal to ** in JavaScript?

不问归期 提交于 2019-12-20 08:12:16
问题 I've just discovered the ECMAScript 7 feature a**b as an alternative for Math.pow(a,b) (MDN Reference) and came across a discussion in that post, in which they apparently behave differently. I've tested it in Chrome 55 and can confirm that the results differ. Math.pow(99,99) returns 3.697296376497263e+197 whereas 99**99 returns 3.697296376497268e+197 So logging the difference Math.pow(99,99) - 99**99 results in -5.311379928167671e+182 . So far it could be said, that it's simply another

How can I use ES2016 (ES7) async/await in my acceptance tests for a Koa.js app?

半世苍凉 提交于 2019-12-19 03:47:05
问题 I am in the process of writing my first Koa.js app, and having recently been introduced to the ES2016 (aka ES7) features of async / await , I wanted to make use of these. I found that my Google skills were not up to the task, and the few snippets of code I could find were either for the standard Koa (using generators) or otherwise not-as-bleeding-edge-as-ES7. See my answer below for how I got my tests running. 回答1: I'm still a beginner, so it's likely that a lot of this can be optimised

How do I destructure all properties into the current scope/closure in ES2015?

江枫思渺然 提交于 2019-12-17 02:38:16
问题 I'd like to do something like this: const vegetableColors = {corn: 'yellow', peas: 'green'}; const {*} = vegetableColors; console.log(corn);// yellow console.log(peas);// green I can't seem to find or figure out how to do this but I really thought I had seen it done somewhere before! :P NOTE: I'm using Babel with stage set to 0 ; CONTEXT: I'm trying to be drier in JSX and not reference this.state or this.props everywhere. And also not have to keep adding properties to destructure if the data

Simple ES7 decorator with babel

人盡茶涼 提交于 2019-12-12 14:21:39
问题 I can not run this code: https://www.npmjs.com/package/core-decorators#readonly I use gulp and babel. I have package.json { "name": "my-first-decorator", "version": "0.0.1", "dependencies": { "core-decorators": "^0.8.1" }, "devDependencies": { "babel-plugin-transform-class-properties": "^6.0.14", "babel-plugin-transform-decorators": "^6.0.14", "babel-preset-es2015": "^6.1.2", "babelify": "^7.2.0", "browserify": "^12.0.1", "gulp": "^3.9.0", "vinyl-source-stream": "^1.1.0" } } and I have

Read a CSV file line-by-line using node.js and ES6/ES7 features

允我心安 提交于 2019-12-12 14:04:53
问题 Reading a CSV file line-by-line (i.e. without loading the whole file into memory) in Python is simple: import csv for row in csv.reader(open("file.csv")): print(row[0]) Doing the same with node.js involves using something like node-csv, streams and callbacks. Is it possible to use new ES6/ES7 features like iterators, generators, promises and async functions to iterate over lines of a CSV file in a way that looks more like the Python code? Ideally I'd like to be able to write something like

Exponentiation operator for Boolean in JavaScript?

和自甴很熟 提交于 2019-12-12 02:28:15
问题 Refer to this, the exponentiation operator returns the result of raising first operand to the power second operand, like the exponentiation operator in Python, which is part of the ECMAScript 2016 (ES7) proposal. We know the result of Boolean with exponentiation operator in Python as following: >>> False ** False == True True >>> False ** True == False True >>> True ** False == True True >>> True ** True == True True I want to know whether the Boolean could be used in the exponentiation

How to stop executing next function with async-await?

大憨熊 提交于 2019-12-12 00:08:49
问题 I'm using this library to chain asynchronous functions in my nodejs app: https://github.com/yortus/asyncawait var chain = async(function(){ var foo = await(bar()); var foo2 = await(bar2()); var foo3 = await(bar2()); } So bar3 waits for bar2 to finish and bar2 waits for bar() to finish. That's fine. But what will I do in order to stop the async block from further execution? I mean something like this: var chain = async(function(){ var foo = await(bar()); if(!foo){return false;} // if bar