Tag: ecmascript next

dynamic引用静态ESNext导入

说我有这些import: import clearLineReporter from '../modules/clear-line-reporter'; import karmaReporter from '../modules/karma-reporter'; import metaTestReporter from '../modules/meta-test-reporter'; import stdReporter from '../modules/std-reporter'; import tapJSONReporter from '../modules/tap-json-reporter'; import tapReporter from '../modules/tap-reporter'; import webSocketReporter from '../modules/websocket-reporter'; 这些必须像我上面做的那样引用,换句话说,我显然不能这样做: const imports = { stdReporter: import(…), tapJSONReporter: import(…), … webSocketReporter: import(…) } 有什么办法可以通过某种forms的reflection来引用导入的文件吗? 因为好像我不能把他们分组在一起,以某种方式引用他们。 我可以使用require() ,而不是import语法,但是我想知道是否有某种方法可以用import语句做一些dynamic的事情,比如dynamic地引用它们,例如,如果我添加或删除一个import,不必更改任何其他代码。

TypeError:Object.entries不是一个函数

为什么试图运行我的Node.js / Express服务器时不断收到此错误? 这是新的ES7的一部分吗? 我需要什么才能使用这些新function来运行应用程序?

Node.js最佳实践exception处理 – asynchronous/等待之后

他们已经是这个话题的一个问题了 Node.js最佳实践exception处理 这是旧的,答案是非常过时的,从那时起, domains甚至被弃用。 现在,在asynchronous/等待Node.js的情况下,我们不应该同时考虑同步和asynchronous情况,并在asynchronous函数中抛出exception,并在asynchronous函数中拒绝承诺,而不是在前一种情况下返回Error实例。 let divideSync = function(x,y) { // if error condition? if ( y === 0 ) { // "throw" the error throw new Error("Can't divide by zero exception") } else { // no error occured, continue on return x/y } } 模拟asynchronous分割操作 let divideAsync = function(x, y) { return new Promise(function(resolve, […]

asynchronous/等待不等待

我遇到了一个我不完全明白的问题。 我觉得有可能是我没有掌握的概念,可以优化的代码,可能还有一个错误的方法。 要大大简化整体stream程: 向外部API发出请求 对返回的JSON对象进行分析并扫描链接引用 如果find任何链接引用,则会使用真正的JSON数据填充/replace链接引用 一旦所有链接引用被replace,原始请求将被返回并用于构build内容 这里是原始请求(#1): await Store.get(Constants.Contentful.ENTRY, Contentful[page.file]) Store.get代表: async get(type, id) { return await this._get(type, id); } 哪些电话: _get(type, id) { return new Promise(async (resolve, reject) => { var data = _json[id] = _json[id] || await this._api(type, id); console.log(data) if(isAsset(data)) { resolve(data); } else if(isEntry(data)) { await this._scan(data); resolve(data); } else […]

dependency injection库 – 重命名注入的值

我想按名称注入lodash,如下所示: let val = function(lodash){ // lodash will be injected, simply by using require('lodash'); }; 但是说我想重命名导入,我想要做这样的事情: let val = function({lodash:_}){ }; 要么 let val = function(lodash as _){ }; 有没有办法做到这一点与ES6 / ES7 / ES8或TypeScript? 请注意,这个DI框架比只需要('x')做更多的工作…它将尝试注入其他值首先,如果没有其他的存在,那么它将尝试要求的价值。 还要注意,这里的要求是,当你调用val.toString()时,“lodash”将被视为参数名称。 但是在函数体内的运行时会看到_而不是lodash。 这是因为为了注入lodash,我们调用fn.toString()来获取参数名称。

如何知道一个函数是否是asynchronous的?

我必须将函数传递给另一个函数,并将其作为callback来执行。 问题是,有时这个函数是asynchronous的,就像: async function() { // Some async actions } 所以我想根据它正在接收的函数的types执行await callback()或callback() 。 有没有办法知道函数的types?

为什么等待不起作用的节点请求模块?

我是nodejs的新手。 我没有看到前1的反应,但我看到前2。为什么? 等待在其他地方为我工作,使用巴贝尔。 例1 let res = await request(url) console.log(res); console.log(res.body); 例2 request(url, function (error, res, body) { if (!error && response.statusCode == 200) { console.log(body) } }); 等待在其他地方工作,我使用babel和es6和es7function所需的模块。 例如,等待在squelize电话的工作,我validation。 但它不适用于请求调用。 为什么?

asynchronous/等待隐式返回诺言?

我读过由async关键字标记的asynchronous函数隐含地返回一个promise: async function getVal(){ return await doSomethingAync(); } var ret = getVal(); console.log(ret); 但是这不是一致的…假设doSomethingAsync()返回一个promise,并且await关键字将从promise中返回值,而不是promise itsef,那么我的getVal函数应该返回这个值,而不是一个隐含的承诺。 那么究竟是什么情况呢? 由async关键字标记的函数隐含地返回promise,还是我们控制返回的内容? 也许如果我们不明确地回报一些东西,那么他们就会隐含地回报一个承诺。 更清楚的是,上面和之间有区别 function doSomethingAync(charlie) { return new Promise(function (resolve) { setTimeout(function () { resolve(charlie || 'yikes'); }, 100); }) } async function getVal(){ var val = await doSomethingAync(); // val is not a promise console.log(val); // logs 'yikes' […]

asynchronous/等待和ES6产出与发电机之间的差异

我正在阅读这篇精彩的文章 – https://www.promisejs.org/generators/ 它清楚地突出了这个函数,它是处理生成器函数的辅助函数: function async(makeGenerator){ return function () { var generator = makeGenerator.apply(this, arguments); function handle(result){ // result => { done: [Boolean], value: [Object] } if (result.done) return Promise.resolve(result.value); return Promise.resolve(result.value).then(function (res){ return handle(generator.next(res)); }, function (err){ return handle(generator.throw(err)); }); } try { return handle(generator.next()); } catch (ex) { return Promise.reject(ex); } } } […]