Tag: ecmascript 7

NodeJS 5.x + Babel 6asynchronous/等待debugging

当我尝试使用transform-async-to-generator babel插件(尽pipe我已经尝试几乎所有其他组合)尝试使用async / awaitdebugging代码时,我遇到了一些棘手的debugging体验。 基本上,一个await的代码会跳到方法的末尾,然后进入编译的代码。 video export class Cat { async meow(){ let p = await this.bat(); // <<<< this line runs this.fart(); // <<<< then skips this line return p; // <<<< and goes to this line ( always last line in fn ) } } 如果您查看该函数的生成代码: meow() { var _this = this; return […]

使用es7 async / await检查文档是否存在于mongodb中

我试图检查用户提供的email提供存在集合的users ,但我的函数不断返回未定义的每个调用。 我使用es6和async/await ,以摆脱大量的callback。 这是我的function(它是在一个类中): async userExistsInDB(email) { let userExists; await MongoClient.connect('mongodb://127.0.0.1:27017/notificator', (err, db) => { if (err) throw err; let collection = db.collection('users'); userExists = collection.find({email: email}).count() > 0; console.log(userExists); db.close(); }); console.log(userExists); return userExists; } 因此, .connect调用中的第一个console.log始终返回false因为.find的返回值不是数组,它是一个巨大的对象,如下所示: { connection: null, server: null, disconnectHandler: { s: { storedOps: [], storeOptions: [Object], topology: [Object] }, […]

能够中止asynchronous调用

我正在使用es7风格的asynchronous/等待方法babeljs。 我有一个主脚本将调用一个对所有返回承诺的对象数组的asynchronous方法。 我使用Promise.all()等待所有这些返回,但是,这些任务可能需要很长时间,如果他们超过了阈值,我想中止所有这些,并以合适的方式处理任务。 有没有办法完成这样的事情? 目前我能想到的唯一方法是产生一个调用这些方法并等待它们全部解决的过程,如果达到了时间限制,它可以终止进程并执行任何需要的处理。 更新:关于主脚本正在等待的这些方法的一些说明…他们可能正在做一连串的操作(调用外部系统,stream文件到某处等),而不是执行一个可以独立取消的单个操作。 更新#2:一些未经testing的半伪代码 class Foo1 { async doSomething() { // call some external system // copy some files // put those files somewhere else (s3) } } class Foo2 { async doSomething() { // Do some long computations // Update some systems } } class FooHandler { constructor() { this.fooList = […]