Tag: asynchronous

asynchronous请求在node.js中的ForEach中

我是新的node.js(和request.js)。 我想从不同path的特定url获取网站正文(在http://www.example.com/path1下方的示例中, http://www.example.com/path2等) )并使用键/值映射(下面的siteData [path])将这些数据logging在一个对象中。 var request = require('request'), paths = ['path1','path2','path3'], siteData = {}, pathLength = paths.length, pathIndex = 0; paths.forEach((path) => { var url="http://www.example.com/"+path; request(url, function(error, response, html){ if(!error){ siteData[path] = response.body; pathIndex++; if(pathIndex===pathLength){ someFunction(siteData); } } }); function someFunction(data){ //manipulate data } 我的问题是: if语句(index ===长度)看起来不像是确定asynchronous请求是否完成的正确方法。 我该如何正确检查请求是否完成? 当我执行上面的代码时,我得到一个错误(node) warning: possible EventEmitter memory leak […]

在等待还是没有返回之间有区别

我只是想着我们需要使用await操作符,以下两种情况有什么区别。 一 public async updateOne(Model, doc, errorMsg?){ return await Model.findOneAndUpdate({'_id': doc._id}, {'$set': doc}, {upsert:true, new: true}).lean().exec(); } 二 public updateOne(Model, doc, errorMsg?){ return Model.findOneAndUpdate({'_id': doc._id}, {'$set': doc}, {upsert:true, new: true}).lean().exec(); } 我认为结果没有什么区别,但是我认为完全没有必要使用async await,因为promise会被返回,我们只需要在asynchronous函数中调用updateOne函数时使用await操作符。

Node.js类模块系统

我试图在node.js中创build一个模块/类来测量asynchronous执行时间,但不明白它有什么问题。 我创build了以下类“Measure.js” var Measure = module.exports = function(param_timeout, param_cb) { this.timeout = param_timeout; this.cb = param_cb; } Measure.prototype = { startDate: "0", timeout:"0", cb:null, start : function() { this.startDate = new Date(); console.log('started'); }, stop : function() { var stopDate = new Date(); this.cb(null,(stopDate-this.startDate)); } } 我用它与下面的代码: var Measure = require('./Measure.js'); measure1 = new Measure(100,function(err,result){console.log('result: […]

Nodejsasynchronous编程 – 为什么需要“asynchronous”模块? 什么是“回拨地狱”/“厄运金字塔”?

NodeJS最大的特点之一就是它与我正在阅读的内容是不asynchronous的,但是作为NodeJS的初学者,如果这个模块已经在本地处理了,为什么会有类似async模块呢? https://www.npmjs.com/package/async 我认为有一个很好的理由,但对我来说并不明显。 是处理callback hell还是Pyramid of Doom ?

nodejs中多个asynchronousmongo请求

如何在一行中编写多个查询? 像一个 Space.findOne({ _id: id }, function(err, space) { User.findOne({ user_id: userid }, function(err, user) { res.json({ space: space, user: user}); }); }); 用更多的请求和逻辑看起来不太好 它是如何正确地完成的? 我听到了诺言,但我不知道。 谢谢

运行npm install时出错:找不到兼容版本:async

我在我的package.json中添加了caolan / async "async": "~0.9.0", asynchronous的npmjs页面在他们的网站上写有“由caolan在7个月前发布的0.9.0”。 当我安装使用npm安装它给我错误 npm ERR! notarget No compatible version found: async@'>=0.9.2-0 <0.10.0-0' npm ERR! notarget Valid install targets: npm ERR! notarget ["0.1.0","0.1.1","0.1.2","0.1.3","0.1.4","0.1.5","0.1.6","0.1.7","0.1.8","0.1.9","0.1.10","0.1.11","0.1.12","0.1.13","0.1.14","0.1.15","0.1.16","0.1.17","0.1.18","0.1.19","0.1.20","0.1.21","0.1.22","0.2.0","0.2.1","0.2.2","0.2.3","0.2.4","0.2.5","0.2.6","0.2.7","0.2.8","0.2.9","0.2.10","0.3.0","0.4.0","0.4.1","0.5.0","0.6.0","0.6.1","0.6.2","0.7.0","0.8.0","0.9.0"] npm ERR! notarget npm ERR! notarget This is most likely not a problem with npm itself. npm ERR! notarget In most cases you or one of your dependencies are […]

用于asynchronousLinux脚本的Node.js

在我写的其他程序中,我使用promise来享受node.js的asynchronous方面。 我想为Linux脚本使用相同的编程风格(使用node.js)。 换句话说,我希望能够同时执行多个Linux命令,然后在这些命令完成之后,我希望node.js脚本asynchronous执行另一组命令(如果没有阻塞)。 我遇到了一个aritlce ,它展示了如何使用node.js执行同步Linux命令,但是我还没有find类似的教程,其中涵盖了使用node.jspipe理多个asynchronousLinux命令。 目前这是可能的吗? 如果是这样,你能指示我一些具体的资源,可以帮助我开始这个目标吗?

Javascript – asynchronous等待vs承诺callback

我正在做一个代码更改将.then(func {})样式代码转换为asynchronous等待。 在我的例子中,从那里转换到asynchronous等待,消除了并行查询API的能力,并按请求完成的顺序处理它们,因为两个请求是相互独立的。 这是两个语法之间的有效区别,还是仅仅是将两个函数分解为两个单独的asynchronous函数将使它们并行运行的问题? 升级之前的示例代码: componentDidMount() { this.loadLists(); } loadLists() { console.log('start 1'); api.get('/url/1').then(function(r) { console.log('done 1', r.body); }); console.log('start 2'); api.get('/url/2').then(function(r) { console.log('done 2', r.body); }); } //OUTPUT //start 1 //start 2 //done 1 //done 2 升级后的示例代码: componentDidMount() { this.getLists(); } async getLists() { console.log('start 1'); var res = await api.get('/url/1'); console.log('done 1', res.body); […]

DeprecationWarning:不build议调用不带callback的asynchronous函数

我正在处理电子+ angularjs。 我开始得到DeprecationWarning: Calling an asynchronous function without callback is deprecated.的问题: DeprecationWarning: Calling an asynchronous function without callback is deprecated. 当我使用id3js从mp3文件中读取标签。 id3({file: pathtofile,type: id3.OPEN_LOCAL}, function(err, tags) { if (err) { console.log(err); } else { console.log(tags);} }); 代码从mp3文件中重新生成标签,但保持生成此弃用警告

asynchronous/等待try / catch在处理逻辑等待解决后不显示错误

当使用try/catch进行async/await请求,并且需要对结果执行一些逻辑时,哪里是做逻辑的最佳位置? 我有这样的function function synchronousTransform (data) { return data.reduce( n => n * 2) } async function requestFn () { try { const myPromisedValue = await axios.get(url) const result = synchronousTransform(myPromisedValue.data) res.status(200).send(result) } catch (xhrError) { res.status(500).send(xhrError) } finally { console.log('done') } } 似乎在synchronousTransform中的东西不工作,我得到的错误。 不过,我在try / catch块中看到的唯一事情就像是XHR的问题。 我如何隔离synchronousTransform的function,以便我可以看到它引起的实际exception?