Tag: ecmascript 6

使用节点中的psql(pg-promise)来承诺循环/ promise.all

你好,我是Promises的新手,并坚持如何等待for循环内的所有承诺,然后去下一个()。 我见过几个promise.all的例子,但我不清楚如何适应他们为我的下面的代码。 它现在转到for循环后的next(),并在for循环完成之前解决。 任何帮助表示赞赏! 我正在使用pg-promise (带有promise的 psql)。 原始码: function getTeamMembers(aTeam) { let promise = new Promise(function(resolve, reject) { db.getTeamMembers(aTeam.tid) //return sql results rows .then(function(rows){ for(let i=0; i<rows.length; ++i) { //loop through each result row getUserByUsername(rows[i].username) .then(function(cfUser) { //add user from row to aTeam object aTeam.addMember(cfUser); }) .catch(function(e) { reject(e); }); } }) .then(function(){ console.log(aTeam); //confirm […]

导入Meteor中的ES6 npm模块时出现SyntaxError错误

我使用meteor create了一个新的meteor项目 我运行了npm install -S spacy-nlp ,其中包含一些ES6代码 在我的server/main.js ,我写import spacy from 'spacy-nlp' 运行meteor ,它抱怨Error: The babel-runtime npm package could not be found in your node_modules. Please run the following command to install it: meteor npm install –save babel-runtime Error: The babel-runtime npm package could not be found in your node_modules. Please run the following command […]

RxJS去嵌套callback

我想做一些事情: Rx.Observable.of(userToken) .flatMap(verifyToken) .flatMap(getUserInformation) .flatMap(createUser) .flatMap(signNewToken) .subcribe({ next: result => useResult(result), error: error => handleError(error) }) 我试图避免的是一个callback混乱。 在我的代码中,像verifyToken这样的verifyToken是Observables,我想链接它们。 这种模式是否正确? 因为现在只要其中一个内部做observer.error(new Error('problem')) ,链崩溃,我的error handling程序不会被调用。 我怎样才能改善这个?

将parameter passing给生成器函数

在过去的几个小时里,我一直坚持这样做。 如何将parameter passing给生成器函数? function* getFoo(foo) { return yield Promise.resolve(foo + 10); } exports.testRoute = Promise.coroutine(function* (req, res) { let bar = yield Promise.coroutine(getFoo); // <— how to pass argument?? res.send(bar.toString()); }); 当前代码抛出错误 (我知道它指向我在这里,但它没有说任何关于parameter passing): Unhandled rejection TypeError: A value [object Promise] was yielded that could not be treated as a promise See http:// goo.gl/4Y4pDk From […]

在nodejs中的父/子类层次结构

child.js class Child { constructor(){ this.helloWorld = "Hello World"; } run() { } } export default new Child(); parent.js import child from './child.js'; class Parent { constructor() { this.child = child; } } export default new Parent(); index.js import parent from './parent.js' console.log(parent.child.helloWorld); <– does not throws an error, displays "Hello World" console.log(parent.child.run); <– throws […]

ES6类和扩展内置缓冲区

我尝试在Nodejs中扩展缓冲区对象 TypeError: Buffer2.from(…).slice2 is not a function 但看起来我从来没有不能用新课程来缠绕 我如何使Buffer2.from返回Buffer2? ES6类和扩展关键字 无论如何更好的方式,而不是一个全新的class级? class Buffer2 extends Buffer { constructor(obj, encoding) { console.log('Buffer2::constructor'); super(obj, encoding); return this; } slice(start, end) { console.log('Buffer2::slice'); return this.slice(start, end) } slice2(start, length) { console.log('Buffer2::slice2'); return this.slice(start, start + length) } static from(obj, encoding) { console.log('Buffer2::from'); return new Buffer2(obj, encoding); } } Buffer2.prototype.slice […]

从类未定义的构造函数中的新对象

我从构造函数中的类创build一个新的对象,每当它运行时,我得到一个错误,操作未定义的方法,虽然它在构造函数中定义。 操作本身是经过彻底的testing,并在单独的环境中工作,所以这不是问题。 我使用Babel构build它, 而不是直接在Node 7.0.0中运行它 import Operate from "./operate" export default class { constructor(Schema) { this.schema = Schema this.operate = new Operate(this.schema) console.log(this.operate.run) // <- Logs just fine } update(req, res) { console.log(this.operate.run) // <- Nada this.operate.run(req.body) .then(value => { res.status(200).json(value) }) } 这感觉就像我错过了一些根本的东西。 我听说这不是一个伟大的模式,所以请随时提出一个更好的方法。 非常感谢。 更新:这是如何使用更新。 我不怀疑这里有任何问题,因为它已经工作得很好,当我从另一个模块导入控制器作为一个function,而不是一个类 import {Router, } from "express" import Controller […]

嵌套在ES6类的方法?

我一直在试图遵循一个DRY编程,我一直在重复自己,所以我试图嵌套方法,帮助一些代码的父方法。 chat() { client.on("chat", (channel, user, message, self) => { method() { // code here } method() { // code here { } } 但是,这并没有按预期的方式调用class.chat.method()没有带回任何东西。 我真正需要帮助的是删除我的DRY编程,我打电话client.on("chat", callback())每一个我使用的方法。 好奇这是否可以防止,只有一个片段与其中调用的方法。 完整代码: watchFor(command, res, sendersName, prefix) { this.client.on("chat", (channel, user, message, self) => { console.log(this._showSendersName.whitelistedCommands); if (message == this.prefix + command || message == prefix + command) { […]

节点js扩展inheritance对象(作为枚举)

看起来好像你不能在Node.js中扩展类对象(在这种情况下用作枚举)。 例 富 class Foo {} Foo.action = {}; Foo.action.jump = 1; module.exports = Foo; 酒吧 var Foo = require('./foo'); var Baz = require('./baz'); class Bar extends Foo { constructor(){ super(); this.baz = new Baz(); } } Bar.action.stay = 2; module.exports = Bar; 用法 (编辑,它在类中使用) var Bar = require('./bar'); class Baz { constructor(){ console.log(Bar.action); //undefined […]

在es2017中,当从asynchronous方法访问时,“this”是未定义的

如何从asynchronous方法调用时引用类实例。 class Senders { callSendAPI() { //Some code here } sendText() { this.callSendAPI(); } async sendCards() { var dbpromise = await db.call(); console.log('this= ', this); this.callSendAPI(); } } export {Senders}; 这=未定义