arrow-functions

Reduce array to object using arrow function

断了今生、忘了曾经 提交于 2019-12-21 06:56:08
问题 I'm playing around with the limits of array and arrow functions and I'm trying to convert this reduce function into an arrow function: var monthsById = months.reduce(function(result, month) { result[month.Id] = month; return result; }, {}); But I'm having trouble to return the map, since result[month.Id] = month; will return the month and not the map like in this approach: var monthsById = months.reduce((byId, month) => byId[month.Id] = month, {}); So I'm looking for a single statement, that

Asynchronous map function that await's returns Promise instead of value

孤街醉人 提交于 2019-12-20 18:30:36
问题 I have this code async function addFiles(dir,tree) { return (await readDir(dir)) .map(async (name) => {await readDir(dir); return name;}) } but unfortunately, it just returns a bunch of promises, because there the async function in map is not waited upon. I'm wondering if there is any way to await the mapped function in the above code. 回答1: try async function addFiles(dir,tree) { const files = await readDir(dir) await Promise.all(files.map(async (name) => {await readDir(dir); return name;}) }

What do the parentheses wrapping the object literal in an arrow function mean? [duplicate]

淺唱寂寞╮ 提交于 2019-12-20 09:00:02
问题 This question already has answers here : ECMAScript 6 arrow function that returns an object (6 answers) What does the arrow function with a () after means? [duplicate] (3 answers) ES6 Fat Arrow and Parentheses `(…) => ({…})` [duplicate] (2 answers) What does arrow function '() => {}' mean in Javascript? [duplicate] (2 answers) Closed 2 years ago . I've seen JavaScript code such as this: let a = () => ({ id: 'abc', name: 'xyz' }) What do the parentheses ( … ) wrapping the object refer to in

Why doesn't the fat arrow bind to this when I pipe my method definition thru an intermediary function?

血红的双手。 提交于 2019-12-20 07:26:52
问题 I have the following code that declares a method which is advised by an intermediary function before the function result is assigned to a prototype slot. class A somemethod: advise => @dosomething() Why does the fat arrow does not bind to the instance in this case? 回答1: Simple answer When an intermediary is put between the prototype slot name and function definition you break the syntactic pattern that makes the CS emit the binding code in the constructor class A foo: (paramlist) => bar: ()=>

nodejs arrow function with expression [duplicate]

落花浮王杯 提交于 2019-12-20 02:27:15
问题 This question already has answers here : ECMAScript 6 arrow function that returns an object (6 answers) Closed 3 years ago . According to the documentation, you can return an expression from an arrow function: (param1, param2, …, paramN) => expression // equivalent to: => { return expression; } but this doesn't seem to work as I would expect (nodejs 4.2.3) > [1,2,3].map(i => i); [ 1, 2, 3 ] > [1,2,3].map(i => {}); [ undefined, undefined, undefined ] Shouldn't the 2nd example return 3 empty

Why can't I access `this` within an arrow function? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-19 21:56:46
问题 This question already has answers here : What does “this” refer to in arrow functions in ES6? (7 answers) Closed 4 years ago . This code below should work as expected, and log "meow" , here an example. function Cat () { this.animalNoise = 'meow' } Cat.prototype.sound = () => { console.log(this.animalNoise) } let cat = new Cat() cat.sound() It doesn't work, this error appears TypeError: Cannot read property 'animalNoise' of undefined and when you convert the arrow function to an actual

react native arrow functions and if statements

爱⌒轻易说出口 提交于 2019-12-19 11:57:07
问题 I have just been schooled on arrow functions, and how they can aid with visibility when you start using sub-functions, here: react native and globally accessible objects I am not sure if this is different for "if" statements, but I can't get this to work at all. The issue: myFunction() { console.log('Welcome Flag: ' + this.props.welcomeFlag); if (this.props.welcomeFlag == false) { this.props.dispatch(setWelcomeFlag(true)); showMessage('Welcome back, ' + this.props.userName + '!', { duration:

How do I refer to actual 'this' inside CoffeeScript fat-arrow callback?

别来无恙 提交于 2019-12-19 10:08:56
问题 The title says it all. When I use the fat-arrow in CoffeeScript, it stores this first before calling the function. For example: class myClass constructor: -> element = $ "#id" element.click -> @myMethod(@value) return return myMethod: (c)-> window.console.log(c) return would yield var myClass; myClass = (function() { function myClass() { var element; element = $("#id"); element.click(function() { this.myMethod(this.value); }); return; } myClass.prototype.myMethod = function(c) { window

Why we don't need to bind the arrow function in React?

孤者浪人 提交于 2019-12-19 02:41:32
问题 We all know that we need to bind function in React to make it work. I do know why do we need to bind it. But I'm not sure why we don't need to bind arrow function. Example: Using arrow function (No bind in required) handleClick = () => { this.setState({ isToggleOn: !this.state.isToggleOn }); }; Now, Using function (Bind is required) this.handleClick = this.handleClick.bind(this); handleClick() { this.setState({ isToggleOn: !this.state.isToggleOn }); }; I'm not asking why we need bind in

Classes with static arrow functions

杀马特。学长 韩版系。学妹 提交于 2019-12-18 20:49:18
问题 I'm currently implementing the static land specification (an alternative of fantasy land). I want to not only use plain objects as types but also ES2015 classes with static methods. I've implemented these static methods as arrow functions in curried form instead of normal functions. However, this isn't possible with ES2015 classes: class List extends Array { static map = f => xs => xs.map(x => f(x)) static of = x => [x] } My map doesn't need its own this , because it is merely a curried