Tag: asynchronous

NodeJS请求中的callback

it('should respond to a GET request at /api/keywords/', function (done) { request.get( { 'url': 'http://localhost:8080/api/keywords/', 'json': true }, function (err, res, body) { expect(res.statusCode).toBe(200); expect(body.foo).toEqual('bar'); done(); }); }); 我正在用Jasminetesting框架在NodeJS上做一个应用程序,同时我也了解callback如何在asynchronous编程中工作的基础知识。 然而,在这个特定的代码片段中,我似乎无法绕过这个想法,即在为url和json传递值之后,request.get()函数中已经传递了一个函数。 我的问题是,像request.get(),我没有定义的函数如何接受callback函数作为参数? 在这个方法中有规定接受这样的参数,因为我没有find一个定义,它声明接受一个callback函数作为参数来执行。 我知道它可以被传递,但是它是如何被执行的,因为我没有写出request.get()的定义,所以我不能定义如何执行一个传递的函数或者甚至执行它。

在NodeJs 8. *如何在http.get上应用Async / Await?

下面的代码从指定的url中asynchronous获取结果,并且在接收到数据后,使用nodejs版本8中的asynchronous/等待(*)(不带callback函数),我想从getData方法中返回parsedvariables。 function getData(v, r) { var path = 'http://some.url.com'; var parsed = ""; http.get({ path: path }, function(res) { var body = ''; res.on('data', function(chunk) { body += chunk; }); res.on('end', function() { parsed = JSON.parse(body); // now I would like to return parsed from this function without making use of callback functions, and make […]

有没有使用async_hooks检索asynchronous堆栈跟踪的任何npm模块?

节点8中有一个名为“async_hook”的新API,我相信应该使模块作者能够打印asynchronous堆栈跟踪。 我正在寻找类似于chrome dev-toolsasynchronous堆栈跟踪实现的东西,但在开发/debugging期间从控制台使用。 我知道这将带来性能开销 – 但这对我来说很好。 有没有人知道任何正在开发的模块,这样做?

如何使用nodemailer为NodeJS应用程序在后台发送电子邮件?

我正在构build一个Angular 4和Node应用程序。 一旦任何用户在前端注册,我将其数据存储在数据库中,然后想要使用nodemailer包向他们发送成功的注册电子邮件。 这是Node js代码: router.post('/', function(req, res, next) { SOME_DATABASE_FUNC() { if(FAILED_CASE) { return res.status(500).json({ title: 'An error occurred', status: 500, error: error }); var mailOptions {…} transporter.sendMail(mailOptions, function (error, info) { if (error) { console.log(error); return res.status(500).json({ title: 'An error occurred', status: 500, error: error }); } console.log('Message', info.messageId, info.response); return res.status(200).json({ message: […]

多个asynchronous/等待try-catch块

我在这里,因为我有麻烦debugging我的应用程序。 这是非常烦人的,不明白为什么我的应用程序崩溃。 我使用承诺(随着/ catch块),但我得到使用asynchronous/等待的必要性。 我有一个方法,我做多个等待它。 这里的问题是,如果我的应用程序崩溃,因为任何原因,我永远不知道它是什么问题。 我已经描述过这样的块: static async processCSGOGroupsAndUsers (groupName) { try{ const csgoApiData = await csgoApi(groupName); const parsedData = await xmltojson(csgoApiData); const id = parsedData.memberList.groupID64; //const members = await retrieveMembers(groupName, parsedData.memberList.memberCount); const totalUsers = await UsersService.processCSGOUsers(id, parsedData); const csgoGroup = { name: parsedData.memberList.groupDetails.groupName, siteUrl: parsedData.memberList.groupDetails.groupURL, id, totalUsers }; await GroupsDao.save(csgoGroup); }catch (err){ return […]

Node.js产生:如何检查进程立即退出

在我的应用程序中,我需要产生一个debugging程序进程给予一个转储文件进行debugging,说“example.dmp”。 如果找不到转储文件,则不是转储文件。 该过程将成功产生,但会立即退出。 我想在asynchronous函数中抛出一个错误消息,稍后可以尝试捕获。 const spawnDebuggerProcess = (debuggerConfigs) => { let debuggerProcess = spawn(config.debuggerName, debuggerConfigs) debuggerProcess.on('exit', (code, signal) => { console.log('pid ' + `${debuggerProcess.pid}`) console.log('child process exited with ' + `code ${code} and signal ${signal}`) }) debuggerProcess.on('error', (error) => {}) if (debuggerProcess.pid !== undefined) { return debuggerProcess } throw externalError.createError({ name: externalError.SPAWN_DEBUGGER_PROCESS_ERROR, description: 'Failed to […]

了解JavaScript中的recursionasynchronous调用

这个问题只是为了好奇。 我知道还有其他的方法来做实际的事情。 我只想学习一些asynchronous模式。 我想从根目录开始search特定的文件。 于是我发现了这个文件模块: https://github.com/mikeal/node-utils/tree/master/file 这个模块提供了一个asynchronous的file.walk(start,callback)函数。 在哪里callback被称为每个find的目录。 想象一下,我希望find具有特定名称的所有文件,将path保存在一个数组中,并稍后对其进行协作处理。 import file = require("file"); var folderArray= []; file.walk(path,function(err,file){ if(doesExist(file+"/mySpecialFileName")){ folderArray.push(file); } }) //when all sub directories are searched do something process(folderArray) 我怎样才能确保所有的目录都被search,我可以进行处理。 例如在潜水模块: https : //github.com/pvorb/node-dive我只是把我的进程(folderArray)在search完成时调用的第三个callback。 非常感谢。 最好的P

如何find哪些promise在nodejs中未处理UnhandledPromiseRejectionWarning?

从版本7的nodejs有asynchronous等待处理承诺的sintactic糖,在我的apis下面的警告经常出现: (node:11057) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: Error: Can't set headers after they are sent. (node:11057) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 不幸的是,没有提到渔获缺失的地方。 有没有办法find它没有检查每个try catch块?

在Array.map函数中使用await

在迭代数组之前,我需要等待一个asynchronous函数完成,我需要等待parsing的asynchronous函数如下: static async sendEmail (from, to, subject, text) { return new Promise((resolve, reject) => { let message = { from, to, subject, text }; AMMailing.transporter.sendMail(message, function (err, response) { if (err) { reject(err); } else { resolve(response); } }); }); } 这是我在数组中迭代的方式的代码,并试图等待它解决再次迭代之前: static sendEmailInQueue (queue) { queue.map(async (person, index) => { console.log('sending email to: ', […]

Typescriptasynchronous生成器,用于套接字

我想了解TSasynchronous生成器。 在Node.js中,我打开一个套接字,从中读取所有数据,然后closures它。 我试图把这个成为一个asynchronous生成器的数据块。 async function* connectAndRead() { const socket = net.connect(80, 'localhost'); socket.on('data', data => ???); socket.on('end', socket.close()); } 有可能做一个asynchronous发生器的数据从套接字读取?