ecmascript-next

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

有些话、适合烂在心里 提交于 2019-12-03 22:08:37
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? 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. let flag1 = true; let flag2 = false; const obj = { requiredKey1: 1, requiredKey2: 2, ...(flag1 && {

How can I use decorators today?

天大地大妈咪最大 提交于 2019-12-03 15:14:27
问题 I see decorators being used today already in some javascript code. My question is really two fold. First: If decorators have not even been finalized how is it possible to use them in production code, today? Won't browser support be non-existent? Second: Given it is possible to use it today, as some open source projects would suggest, what's a typically recommended setup for getting decorators to work? 回答1: You're right, ES2016 decorators are not yet part of the spec. But it doesn't mean we

Babel support for Object.entries

蹲街弑〆低调 提交于 2019-12-03 14:55:10
问题 I'm looking at the stage 3 proposal of Object.values/Object.entries and I'd really like to use it in my current JavaScript project. However, I can't figure out whether there's any Babel preset which supports it. Since the GitHub repository linked above says it's a stage 3 proposal, I assumed it would be part of babel-preset-stage-3, but it seems not. Is there any Babel preset (or even plugin?) that lets me use Object.entries today? 回答1: Using babel, installing babel-preset-env babel-plugin

JavaScript array .reduce with async/await

柔情痞子 提交于 2019-12-03 05:32:02
问题 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

Node.js assert.throws with async functions (Promises)

喜夏-厌秋 提交于 2019-12-03 05:30:17
I want to check if an async function throws using assert.throws from the native assert module. I tried with const test = async () => await aPromise(); assert.throws(test); // AssertionError: Missing expected exception.. It (obvioulsy?) doesn't work because the function exits before the Promise is resolved. Yet I found this question where the same things is attained using callbacks. Any suggestion? (I'm transpiling to Node.js native generators using Babel) Bergi node 10 and newer Since Node.js v10.0, there is assert.rejects which does just that Older versions of node async functions never throw

While loops using Await Async.

℡╲_俬逩灬. 提交于 2019-12-03 05:25:27
This Javascript function seems to use the while loop in an asynchronous way. Is it the correct way to use while loops with asynchronous conditions? var Boo; var Foo = await getBar(i) while(Foo) { Boo = await getBar3(i) if (Boo) { // something } Foo = await getBar(i) i++ } What I think it does is this: var Boo; var Foo; getBar(i).then( (a) => { Foo = a; if(Foo) { getBar3(i).then( (a) => { Boo = a if(Boo) { //something i++; getBar(i).then( (a} => { Repeat itself...} } } } }) If that's totally false could you show another way to do it with async await + while loop? Thanks!! Is it the correct way

How can I use decorators today?

≯℡__Kan透↙ 提交于 2019-12-03 04:51:35
I see decorators being used today already in some javascript code. My question is really two fold. First: If decorators have not even been finalized how is it possible to use them in production code, today? Won't browser support be non-existent? Second: Given it is possible to use it today, as some open source projects would suggest, what's a typically recommended setup for getting decorators to work? You're right, ES2016 decorators are not yet part of the spec. But it doesn't mean we can't use it today. First let's take a step back and go over "what is a decorator". Decorators are simply

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

偶尔善良 提交于 2019-12-03 03:11:52
问题 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,

Difference between async/await and ES6 yield with generators

眉间皱痕 提交于 2019-12-03 02:42:21
问题 I was just reading this fantastic article «Generators» and it clearly highlights this function, which is a helper function for handling generator functions: function async(makeGenerator){ return function () { var generator = makeGenerator.apply(this, arguments); function handle(result){ // result => { done: [Boolean], value: [Object] } if (result.done) return Promise.resolve(result.value); return Promise.resolve(result.value).then(function (res){ return handle(generator.next(res)); },

Decorators on functions

三世轮回 提交于 2019-12-02 20:01:54
I see that babel.js decorators (available in "stage 1") implement the spec at https://github.com/wycats/javascript-decorators . It appears that decorators are limited to (1) classes, (2) accessors, and (3) methods. In my case, I want to use decorators on plain old functions , as in @chainable function foo() { } where (just an example) function chainable(fn) { return function() { fn.apply(this, arguments); return this; }; } I don't see any logical reason why decorators should not be able to apply to functions. My question is, is there some way to accomplish this? Or is there some good reason