Tag: ecmascript 2017

节点repl与asynchronous等待

我想添加对asynchronous/等待到节点repl的支持 在这个问题之后: https : //github.com/nodejs/node/issues/8382 我试图使用这一个https://github.com/paulserraino/babel-repl但它是缺lessasynchronous等待suppport 我想用这个片段 const awaitMatcher = /^(?:\s*(?:(?:let|var|const)\s)?\s*([^=]+)=\s*|^\s*)(await\s[\s\S]*)/; const asyncWrapper = (code, binder) => { let assign = binder ? `root.${binder} = ` : ''; return `(function(){ async function _wrap() { return ${assign}${code} } return _wrap();})()`; }; // match & transform const match = input.match(awaitMatcher); if(match) { input = `${asyncWrapper(match[2], match[1])}`; } […]

尝试在摩卡中使用asynchronous/等待

我想在摩卡中使用asynchronous/等待,以使我的testing。 我已经阅读了很多文章,但是我没有find解决scheme。 我已经安装了所有的babel模块来编译代码,但是它不起作用。 这里是我的代码里面的“testing”文件夹: import test from 'mocha' import 'babel-polyfill' import { expect } from 'chai' import { assert } from 'chai' import utils from '../lib/utils' describe('long number', function () { it("Sample", mochaAsync(async () => { var x = utils.longNums(0); expect(x).to.equal(5000); })) }) 这里是我的package.json,我使用所有的babel依赖和插件,我已经阅读我必须安装,我的testing脚本,我build议摩卡使用巴贝尔transpiling { "name": "pos_lisa-test", "version": "1.0.0", "description": "pos lisa test", "main": "index.js", […]

链接asynchronousfunction?

let val = 0; async function first() { console.log('1a', val); second(); console.log('1b', val); } async function second() { console.log('2a', val); third(); console.log('2b', val); } async function third() { console.log('3a', val); val = await new Promise(function (resolve, reject) { setTimeout(function () { resolve(3); }, 1000); }); console.log('3b', val); } console.log('0a', val); first(); console.log('0b', val); 我期待: 0a […]

使用await时意外的标识符

我目前正在尝试使用asynchronous/等待一个函数,要求循环是同步的。 这是function: async channelList(resolve, reject) { let query = ['channellist'].join(' '); this.query.exec(query) .then(response => { let channelsRaw = response[0].split('|'); let channels = []; channelsRaw.forEach(data => { let dataParsed = ResponseParser.parseLine(data); let method = new ChannelInfoMethod(this.query); let channel = await method.run(dataParsed.cid); channels.push(channel); }); resolve(channels); }) .catch(error => reject(error)); } 当我尝试运行它时,我得到这个错误: let channel = await method.run(dataParsed.cid); ^^^^^^ […]

我怎样才能在顶层使用asynchronous/等待?

我一直在asynchronous/等待,经过几篇文章,我决定自己testing一下。 然而,我似乎无法绕过我的头为什么这是行不通的: async function main() { var value = await Promise.resolve('Hey there'); console.log('inside: ' + value); return value; } var text = main(); console.log('outside: ' + text) 控制台输出以下内容(节点v8.6.0): >外:[object promise] >里面:嘿,那里 为什么函数内部的日志消息之后执行? 我认为asynchronous/等待创build的原因是为了执行使用asynchronous任务的同步执行。 有没有办法可以使用函数内返回的值,而不使用main()后面的.then() main() ?

asynchronousfunction – 等待不等待承诺

我试图学习asynchronous等待。 在这个代码中 – const myFun = () => { let state = false; setTimeout(() => {state = true}, 2000); return new Promise((resolve, reject) => { setTimeout(() => { if(state) { resolve('State is true'); } else { reject('State is false'); } }, 3000); }); } const getResult = async () => { return await myFun(); } […]

从发生器迁移到asynchronous/等待

我刚刚来到痛苦的认识,发电机function不能用于等待。 只承诺或asynchronousfunction。 我的团队使用由发生器函数组成的所有模块构build了一个完整的应用程序,并从主js文件调用Co模块。 除了通过数百个生成器函数并将它们从function*(…){改为async function(…){ ,还有什么可以使生成器与asynchronous/等待一起工作呢? 没有任何意义,因为yield * / generator和async / await在处理stream程方面非常相似,所以我想知道他们是如何错过了等待支持生成器的。

如何在Heroku上启用ES2017function来运行Node.js应用程序?

我是新来的节点,并创build了一个应用程序,它有一些async / await语法就像这样: const express = require('express'); const app = express(); const someLibrary = require('someLibrary'); function asyncWrap(fn) { return (req, res, next) => { fn(req, res, next).catch(next); }; }; app.post('/getBlock', asyncWrap(async (req,res,next) => { let block = await someLibrary.getBlock(req.body.id); [some more code] })); app.listen(process.env.PORT || 8000); 它在我的机器上工作正常,但是当我部署到Heroku我得到一个错误,因为语法不支持: 2017-03-23T10:11:13.953797+00:00 app[web.1]: app.post('/getBlock', asyncWrap(async (req,res,next) => { 2017-03-23T10:11:13.953799+00:00 […]

尝试/ catchasynchronous/等待块

我正在挖掘节点7asynchronous/等待function,并像这样在代码中徘徊 async function main() { try { var quote = await getQuote(); console.log(quote); } catch(error) { console.error(error); } } 这似乎是唯一的可能性解决/拒绝或返回/抛出asynchronous/等待,但是,V8不优化代码try / catch块? 有替代品吗?