Tag: async await

即使在捕获之后,Node.js仍然处于未处理状态

我正在使用节点7.2.1与新的asynchronous/等待function。 我也用这样的mongoose使用本地ES6诺言 – const mongoose = require('mongoose'); mongoose.Promise = global.Promise; 我的代码stream是这样的 – async function getFollowers(){ try { const followers = await User.getFollowersFromMongo(req.params.userId); res.send(followers); } catch (err) { winston.error('Printing Error = ', err); res.status(400).send({success: false, error: err}); } } UserSchema.statics.getFollowersFromMongo = async(userId) => { try { let aggregateQuery = []; //some syntactical error in mongo query […]

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, […]

是否有可能在节点7中使用–harmony_async_await跨asynchronous/等待边界获取堆栈跟踪?

我们正在尝试在节点7中使用–harmony_async_await,并且与使用babel进行asynchronous/等待的传输进行比较,却缺less了具有长堆栈跟踪的function( http://bluebirdjs.com/docs/api/promise.longstacktraces.html )。 显然,如果有一个选项可以“只看到这个” – 在chrome(ium)方面,它们似乎支持它在检查器中,但是最新的工作是在v8 5.4之后(即不在节点7 ): https://bugs.chromium.org/p/v8/issues/detail?id=4483 https://codereview.chromium.org/2357423002/ 另一种方法是让v8使用蓝鸟的诺言实现 – 但我怎么能做到这一点? 重写global.Promise不会改变async / await的行为(他们仍然使用默认的promise)。

当从C#调用Edge.js时,如何钩住stdout和stderr?

背景 我正在通过Process.Start()运行Node的C#程序。 我从这个subprocess捕获stdout和stderr,并根据我自己的原因redirect它。 我正在考虑用一个调用Edge.js代替Node.exe的调用。 为了能够做到这一点,我必须能够可靠地捕获在Edge中运行的Javascript的标准输出和标准错误,并将消息传回到我的C#应用​​程序中。 方法1 我会描述这种方法的完整性,以防有人推荐它:) 如果Edge进程终止,则只需简单地声明一个msgs数组,并用新的函数覆盖process.stdout.write和process.stderr.write来处理这个问题是相当容易的,这些函数会在该数组上累积消息,最后只需返回消息数组。 例: var msgs = []; process.stdout.write = function (string) { msgs.push({ stream: 'o', message : string }); }; process.stderr.write = function (string) { msgs.push({ stream: 'e', message: string }); }; // Return to caller. var result = { messages: msgs; …other stuff… }; callback(null, result); 显然这只有在Edge代码终止的情况下才有效,在最坏的情况下,消息可能会变大。 但是,它很可能会performance良好,因为只有一个编组调用才能获取所有消息。 […]

asynchronous等待nodejs 7

我已经安装了nodejs 7.3.0,我有这样的代码: let getContent = function (url) { // return new pending promise return new Promise((resolve, reject) => { // select http or https module, depending on reqested url const lib = url.startsWith('https') ? require('https') : require('http'); const request = lib.get(url, (response) => { // handle http errors if (response.statusCode < 200 || response.statusCode > […]

如何在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 […]

节点在asynchronous函数完成之前退出

我有一个函数返回一个承诺,我试图从一个asynchronous函数中等待它。 问题是程序立即完成,而不是等待承诺。 asynchronoustest.js: function doItSlow() { const deferred = new Promise(); setTimeout( () => { console.log( "resolving" ); deferred.resolve(); }, 1000 ); return deferred; } async function waitForIt( done ) { console.log( "awaiting" ); await doItSlow(); console.log( "awaited" ); done(); } waitForIt(() => { console.log( "completed test" ); }); console.log( "passed by the test" ); […]

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

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

Intellij Idea警告 – 用aysnc / await“返回的Promise被忽略”

我使用Node.js v7.3在我的代码中使用Express.js 。 在此我创build了一个User Router ,将请求转发给我的User Controller 。 我在User Controller内部使用async / await来执行asynchronous调用。 问题是IntelliJ给了我一个警告 从login()返回的Promise被忽略。 事情是我甚至没有从login()方法返回任何东西。 这是代码 – UserRouter.js router.post('/login', function (req, res, next) { userController.login(req, res); // I get the warning here }); UserController.js exports.login = async function (req, res) { try { const verifiedUser = await someFunction(req.body.access_code); let user = await User.findOrCreateUser(verifiedUser); res.status(200).send(user); } […]

超时在asynchronous/等待

我与Node.js和TypeScript,我正在使用async/await 。 这是我的testing用例: async function doSomethingInSeries() { const res1 = await callApi(); const res2 = await persistInDB(res1); const res3 = await doHeavyComputation(res1); return 'simle'; } 我想为整体function设置一个超时时间。 即如果res1需要2秒, res2需要0.5秒, res3需要5秒我想有一个超时,3秒后,让我抛出一个错误。 使用正常的setTimeout调用是一个问题,因为范围已经丢失: async function doSomethingInSeries() { const timerId = setTimeout(function() { throw new Error('timeout'); }); const res1 = await callApi(); const res2 = await persistInDB(res1); const res3 […]