ecmascript-harmony

Getting a promise's value via yield & co

拥有回忆 提交于 2019-11-29 06:23:23
I'm trying to figure out how to get the value of a promise via yield , possibly with "co": function *(){ var someVar = yield functionThatReturnsAPromise(); } The called function is not a generator, just a normal function. With the above, someVar == Promise , but I want the resolved value. Does co or some other library have a way of doing this? Yes, co can do that. You'll have to wrap parent function inside co call: co(function *(){ var someVar = yield functionThatReturnsAPromise(); })() someVar inside will become resolved value. If promise gets rejected, error can be cought with basic try {}

node.js / ES6 / class creation : SyntaxError: Unexpected reserved word

核能气质少年 提交于 2019-11-29 03:29:49
I try to create a class on my node.js / express app. It works in basic js / prototype mode such as : function MyClass() { /* constructor code */ }; MyClass.prototype.myMethod = function() { /* method code */ }; module.exports = MyClass; But I want to do use the class, constructor, extends, ... keywords. I've try that : class MyClass { constructor() { /* constructor code */ } myMethod() { /* method code */ } } But it doesn't work, the error is : class MyClass { ^^^^^ SyntaxError: Unexpected reserved word My command line to launch the app with all harmony options : node `node --v8-options | grep

Why can functions be called without parentheses when using template strings? [duplicate]

自作多情 提交于 2019-11-29 02:03:32
问题 This question already has answers here : Backticks calling a function (2 answers) Closed 2 years ago . I have a simple logging function: function log(str) { console.log('logged: ', str); } If I call it without parentheses (currently using Chrome's dev tools) and pass in a template string, like this: log`foo` The output is: logged: ["foo", raw: Array[1]] If I call it with parentheses, log(`foo`) The output is: logged: foo Why does calling a function using a template string an no parentheses

Implementing monads in JavaScript

眉间皱痕 提交于 2019-11-28 23:26:10
Now that node.js supports ECMAScript Harmony generators we can write monadic code succinctly ala do blocks in Haskell: function monad(unit, bind) { return function (f) { return function () { var g = f.apply(this, arguments); return typeOf(g) === "Generator" ? send() : unit(g); function send(value) { var result = g.next(value); if (result.done) return unit(result.value); else return bind(result.value, send); } }; }; } function typeOf(value) { return Object.prototype.toString.call(value).slice(8, -1); } In the code above monad is a function which can be used to create deterministic monads like:

Splitting up class definition in ES 6 / Harmony

做~自己de王妃 提交于 2019-11-28 21:38:20
Suppose I have a class in one big file like this: export default class { constructor () {} methodA () {} methodB () {} methodC () {} } And I want to break up the class definition so that methodA , methodB , and methodC are each defined in their own separate files. Is this possible? You should be able to, as class is supposed to just be syntax sugar for the usual prototype workflow: import methodOne from 'methodOne' import methodTwo from 'methodTwo' class MyClass { constructor() { } } Object.assign(MyClass.prototype, {methodOne, methodTwo}) export default MyClass @elclanrs gave a correct answer

Map using tuples or objects

三世轮回 提交于 2019-11-28 18:39:40
I'm trying to use the new (ES6) Map objects in order to represent a map between properties and a value. I have objects in a form similar to: {key1:value1_1,key2:value2_1},..... {key1:value1_N,key2:value2_N} I want to group them based on both their key1 and key2 value. For example, I want to be able to group the following by x and y : [{x:3,y:5,z:3},{x:3,y:4,z:4},{x:3,y:4,z:7},{x:3,y:1,z:1},{x:3,y:5,z:4}] And obtain a Map containing: {x:3,y:5} ==> {x:3,y:5,z:3},{x:3,y:5,z:4} {x:3,y:4} ==> {x:3,y:4,z:4},{x:3,y:4,z:7} {x:3,y:1} ==> {x:3,y:1,z:1} In Python, I'd use tuples as dictionary keys. ES6

When should I use let and var? [closed]

拜拜、爱过 提交于 2019-11-28 16:27:37
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . EDIT: Please read the question! I already know the difference. This is not a duplicate. Obviously, right now I should always be using the var key word as let isn't supported in everything. When the let keyword has better support (say, I'm writing a Node application in a

ECMAScript:Harmony / ES6 to JavaScript compiler

百般思念 提交于 2019-11-28 15:39:32
After reading Peter's article on JavaScript I noticed Brendan Eich stated that one the goals for Harmony is to be a better target for to-JavaScript compilers. There are currently two popular compilers with some vague ES:Harmony compliance: Traceur CoffeeScript Although CoffeeScript has some compliance it's not designed to be an ES:Harmony compiler so it's not useful to this end. Tracuer seems to be sticking more rigorously to the ES:Harmony specification but I don't know whether it intends to become a full ES:Harmony compiler. Since the aim is to to compile ES6 down to ES3 it would also need

ES6 export default AssignmentExpression

笑着哭i 提交于 2019-11-28 14:40:26
export default var foo = {...} It's AssignmentExpression and valid es6 syntax? JSHint says it Expected an identifier and instead saw 'var'. On last spec I not found any relation with VariableStatement and AssignmentExpression. var foo = {...} is not an AssignmentExpression . AssignmentExpression is almost the top level non-terminal symbol that represents all expression , i.e. basically every expression is an AssignmentExpression . var only appears in a variable declaration or a for loop, so drop the var . 来源: https://stackoverflow.com/questions/24925628/es6-export-default-assignmentexpression

JavaScript native Promise() without callback

↘锁芯ラ 提交于 2019-11-28 14:25:20
Look at this jQuery code: var promise = new Deferred(), some; some = function(promise) { // do cool things promise.resolve(); }; promise.then(/* callback cool things */); // init everything some(promise); I am not sure about architecture correctness of such approach, but I used it for long time and it is convenient for me. In native JavaScript I can not use such approach. Constructor new Promise() requires a callback parameter, so I can not pass instance of Promise as a parameter. So my question is: how can I predefine JavaScript native promise, pass it as a parameter to function and the