ecmascript-next

JavaScript array .reduce with async/await

半世苍凉 提交于 2019-12-02 17:53:20
Seem to be having some issues incorporating async/await with .reduce(), like so: const data = await bodies.reduce(async(accum, current, index) => { const methodName = methods[index] const method = this[methodName] if (methodName == 'foo') { current.cover = await this.store(current.cover, id) console.log(current) return { ...accum, ...current } } return { ...accum, ...method(current.data) } }, {}) console.log(data) The data object is logged before the this.store completes... I know you can utilise Promise.all with async loops, but does that apply to .reduce() ? Bergi The problem is that your

What is the shortest way to modify immutable objects using spread and destructuring operators

纵然是瞬间 提交于 2019-12-02 16:39:37
I'm looking for a pure function, to modify my immutable state object. The original state given as parameter must stay untouched. This is especially useful when working with frameworks like Redux and makes working with immutable object in javascript much easier. Especially since working with the object spread operator using Babel is already possible. I did not found anything better than first copy the object, and than assign/delete the property I want like this: function updateState(state, item) { newState = {...state}; newState[item.id] = item; return newState; } function deleteProperty(state,

Events vs Streams vs Observables vs Async Iterators

故事扮演 提交于 2019-12-02 13:55:18
Currently, the only stable way to process a series of async results in JavaScript is using the event system. However, three alternatives are being developed: Streams: https://streams.spec.whatwg.org Observables: https://tc39.github.io/proposal-observable Async Iterators: https://tc39.github.io/proposal-async-iteration What are the differences and benefits of each over events and the others? Do any of these intend to replace events? There are roughly two categories of APIs here: pull and push. Pull Async pull APIs are a good fit for cases where data is pulled from a source. This source might be

Spread syntax ES6 with statement

杀马特。学长 韩版系。学妹 提交于 2019-12-02 11:42:18
I tried to write ternary operator with spread syntax and copy two objects. Is it possible to use ternary operator with spread syntax inside with literal objects? My code works okay, I just want to optimize it. hintStyle: disabled ? {...globalStyles.hint, ...globalStyles.hintDisabled} : globalStyles.hint, I want to write like this: hintStyle: {...globalStyles.hint, {disabled ? ...globalStyles.hintDisabled : {}}}, Spread is not an operator , it's part of the object literal syntax (or at least it will be when the proposal is accepted). You need to write {...globalStyles.hint, ...(disabled ?

Arrow functions as class properties using Babel

久未见 提交于 2019-12-01 17:10:37
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 can't clearly understand if Babel is supporting them or not. Is there some plug-in? UPDATE: I can see

Destructuring object and ignore one of the results

泪湿孤枕 提交于 2019-12-01 15:08:59
I have: const section = cloneElement(this.props.children, { className: this.props.styles.section, ...this.props, }); Inside this.props , I have a styles property that I don't want to pass to the cloned element. How can I do? You can use the object rest/spread syntax : // We destructure our "this.props" creating a 'styles' variable and // using the object rest syntax we put the rest of the properties available // from "this.props" into a variable called 'otherProps' const { styles, ...otherProps } = this.props; const section = cloneElement(this.props.children, { className: styles.section, // We

JS & ES6: Access static fields from within class

為{幸葍}努か 提交于 2019-12-01 04:10:27
In ES6, given the following example: export default class MyStyle extends Stylesheet { static Color = { mainDark: '#000' } static Comp = { ... color: Color.mainDark } } How can I access Color.mainDark (the static field)? Dominic I assume you mean ES.next (probably using babel stage-0) as klaemo said you can access it as you would expect, however if I recall there were some issues when using Babel and exporting the class immediately, so export after defining the class if you're having problems: class MyStyle extends Stylesheet { static Color = { mainDark: '#000' } someMethod() { console.log

JS & ES6: Access static fields from within class

不想你离开。 提交于 2019-12-01 02:29:57
问题 In ES6, given the following example: export default class MyStyle extends Stylesheet { static Color = { mainDark: '#000' } static Comp = { ... color: Color.mainDark } } How can I access Color.mainDark (the static field)? 回答1: I assume you mean ES.next (probably using babel stage-0) as klaemo said you can access it as you would expect, however if I recall there were some issues when using Babel and exporting the class immediately, so export after defining the class if you're having problems:

Is it OK to use async/await almost everywhere?

人盡茶涼 提交于 2019-11-30 19:43:33
I'm currently writing small NodeJS CLI tool for personal usage and I've decided to try ES7 async/await feature with Babel. It's a network tool so I obviously have asynchronous network requests. I wrote a simple wrapper for request package: export default function(options) { return new Promise(function(resolve, reject) { request({...options, followAllRedirects: true, headers: { "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" } }, (error, response, body) => { if(error) { return reject(error); } resolve({response: response, body: body}); }); }); } Now I

What should happen with `await` when the expression after the keyword does not evaluate to promise?

那年仲夏 提交于 2019-11-30 02:59:14
问题 I have a ES7 code like this. async function returnsfive() { var three = 3; var threeP = await three; return threeP+2; } returnsfive().then(k=>console.log(k), e=>console.error("err", e)) What should happen at the var threeP = await three line? Should the code continue as expected, or fail, because three is not a promise? In this repo, it is mentioned as "Debatable Syntax & Semantics". I am not able to read through the official documentation to find the exact definition, since it's too