Tag: ecmascript 6

所有承诺解决后执行承诺

我已经写了一些遍历目录的代码,选取所有的jpg文件,重命名它们,创build拇指文件夹,并将它们放在该文件夹中。 我也编写了一个函数,每个图像生成一个html块,在forEach循环结束时应该把所有的输出写入一个文件。 // Read the directory fs.readdir(dir, function (err, files) { // Return the error if something went wrong if (err) { console.log("Something went wrong"); } //Make thumbs dir fs.mkdirSync(thumbsFolder); // For every file in the list files.forEach(function (file) { var extension = path.extname(file); if (extension === ".jpg") { //Rename images var renameTask = renameImages(file, […]

如何在Open Shift中修改节点启动命令?

我使用ES6和babel-node来创build我的应用程序,我需要我的应用程序以命令babel-node app.js 。 该命令在脚本中列出:在我的package.json中启动,所以命令npm start运行正确的命令。 打开shift会启动节点应用程序,并在package.json文件的主属性中设置脚本。 在我的情况下,它的"main": "app.js" 。 所以这个命令运行node app.js 服务器在遇到的第一个ES6上感到窒息,这是有道理的。 我不知道如何configurationopenshift运行babel-node或npm启动启动我的应用程序。 这里是我的package.json文件 – > https://gist.github.com/jkinman/2cc57ce5fae5817d6bca

当使用文件的绝对path时,babel-node失败

import { Observable, } from 'rx'; import { readFile, } from 'fs'; let readFile_ readFile_ = Observable.fromNodeCallback(readFile) readFile_(__dirname + '/my-app.js', 'utf-8') .subscribe( (text) => console.log(text), (err) => console.error(err) ) 当我运行这个ecma6作为babel-node /home/sk/ws/rxjs-workshop-nov-2015/clcn-app/my-app.js ,它失败,错误, /usr/local/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:329 throw new Error("Couldn't find preset " + JSON.stringify(val)); ^ Error: Couldn't find preset "es2015" 但是当我cd到文件的父文件夹,并运行babel-node my-app.js ,它的工作原理。 为什么babel-node有这个限制?

ES6多重inheritance?

有人可以解释下面有效的ES6代码的含义吗? 'use strict'; class first { constructor() { } } class second { constructor() { } } class third extends (first, second) { constructor() { super(); } } 据我所知,在JavaScript中没有多重inheritance,但该示例中显示的语法不会抛出任何错误(在Node.js 4.3.0),它的工作原理,… – 试图理解,或者它在那里做什么… 另外,我注意到如果我注释掉了super()调用,那么代码开始抛出错误ReferenceError: this is not defined 。

汇总包括不需要的依赖关系,并在输出中缺less一个函数

我第一次使用rollup ,它产生了一些意想不到的结果。 下面我有我的例子中的三个文件,以及来自rollup的输出和我正在寻找的输出种类。 这是我的完整示例的回购。 我有三个文件01.js , 02.js , 03.js 。 01.js import { fakePromise } from './02' export default fakePromise 02.js import { map } from 'lodash' import { stupidReference } from './03' export function fakePromise (str) { return stupidReference(str) } export function fakeMap (arr) { return map(arr, item => item + ' is stupid') […]

如何在Node中使用for..of迭代map的key和values?

我正在使用Node v5.4.1,并且我无法使用MDN上概述的for..of循环来对地图的键和值进行交互。 使用下面的代码: var map = new Map(); map.set(1, 'hello'); map.set(2, 'world'); for (var [key, value] of map.entries()) { console.log(key + " = " + value); } 结果在语法错误: for (var [key, value] of map.entries()) { ^ SyntaxError: Unexpected token [

导出函数不是函数?

不知道为什么不把它看作一个函数: impl.js export default function(callback){ return callback(); }; test.js import {myModule} from '../../src/impl.js' import {expect} from 'chai'; const module = myModule; describe('', () => { it('should callback when resolve is invoked', () => { module(resolve => { resolve('test'); }).then(value => { expect(value).to.equal('test'); }); }); }); 错误: TypeError: module is not a function

如何使用Mocha with Promises / ES6生成器?

我想用ES6 generators使用Mocha ,也可以随意使用。 经过一番研究,我构build了以下代码片段: describe('test', function() { it('should not work', function() { expect(function() { return co(function*() { yield Service.validate(); }); }).to.throw(new Error('error')); }); }); Service.validate抛出new Error('error') ,这就是我所expect 。 module.exports = { validate: function() { return co(function*() { // use yield to access db and so on throw new Error('error'); }); } }; 但是,下面的代码抛出AssertionError: expected [Function] […]

在NodeJS中调用ES6callback中的基类

例如,我有以下类: class X extends Y { constructor() { super(); } method() { asyncMethod( function( err ) { super.method( err ); } ); } } 但是, super不是基类Y 我怎么能通过supercallback? 有没有比使用箭头function有任何解决scheme?

调用super()的子类javascript的错误

我正在尝试学习ES2015 JavaScript类 ,我开始这样的代码: 文件: index.js /* parent class */ class Thing { construct(){ console.log("thing constructor"); } } /* child class */ const Human = class Human extends Thing { construct(){ super(); } } let Person = new Human(); 文件: package.json { "scripts": { "serve": "nodemon index.js –exec babel-node" }, "dependencies": { "babel-cli": "^6.9.0", "babel-preset-es2015": "^6.9.0" […]