Tag: asynchronous

节点js +多个嵌套的内部函数与callback

我已经与单一函数(abcd)中的callback嵌套内部函数。 我需要使用asynchronous从外部调用abcd函数并返回响应。 var listFunctions = { test1 : function(objectData, callbackData) { //have logic and retrun data callbackData(null, "a"); }, test2 : function(objectData, callbackData) { //have logic and retrun data callbackData(null, "b"); }, test3 : function(objectData, callbackData) { //have logic and retrun data callbackData(null, "c"); }, test4 : function(objectData, callbackData) { //have logic and retrun data […]

从asynchronous获取蓝鸟承诺等待function

我正在寻找一种方法,使用Node v7.6或更高版本,在调用asynchronous函数时获取Bluebird Promise(或任何非本地promise)。 我可以这样做: global.Promise = require('Bluebird'); // Or Q/When var getResolvedPromise = () => Promise.resolve('value'); getResolvedPromise .tap(…) // Bluebird method .then(…); 请参阅: 我可以使用global.Promise = require(“bluebird”) 我希望能够做到这样的事情: global.Promise = require('Bluebird'); // Or Q/When var getResolvedAsyncAwaitPromise = async () => 'value'; getResolvedAsyncAwaitPromise() .tap(…) // Error ! Native Promises does not have `.tap(…)` .then(…); 我知道我可以随时使用这样的东西: Bluebird.resolve(getResolvedAsyncAwaitPromise()) .tap(…); […]

如何在Node.js中为非常大(> 1GB)的文件的每一行运行一个asynchronous函数

假设你有一个巨大的(> 1GB)loggingID的CSV: 655453 4930285 493029 4930301 493031 … 对于每个id您都希望进行REST API调用以获取logging数据,将其转换为本地数据,然后将其插入到本地数据库中。 你如何做到这一点与Node.js的可读Stream ? 我的问题基本上是这样的:你如何逐行阅读一个非常大的文件,为每一行运行一个asynchronous函数,并且[可选地]能够从特定行开始读取文件? 从下面的Quora问题我开始学习使用fs.createReadStream : http://www.quora.com/What-is-the-best-way-to-read-a-file-line-by-line-in-node-js var fs = require('fs'); var lazy = require('lazy'); var stream = fs.createReadStream(path, { flags: 'r', encoding: 'utf-8' }); new lazy(stream).lines.forEach(function(line) { var id = line.toString(); // pause stream stream.pause(); // make async API call… makeAPICall(id, function() { // then […]

在NodeJS中同步less编译

我正在尝试为Browserify编写一个转换脚本,允许我使用require() .less文件。 转换将它们编译为CSS,然后将该缩小的CSS封装在一个将CSS附加到页面的小函数中。 我的麻烦是, 主LESS模块是asynchronous的,这似乎不是与转换脚本一起工作: lessify / index.js (直接从node-underscorifybuild模) var less = require('less'); var cleanCSS = require('clean-css'); var through = require('through'); module.exports = function(file) { if (!/\.css|\.less/.test(file)) { return through(); } var buffer = ""; return through(function(chunk) { return buffer += chunk.toString(); }, function() { compiled = buffer; if (/\.less/.test(file)) { compiled = less.render(compiled, function(e, […]

在Node.jsdevise模式中释放zalgo为什么asynchronouspath一致?

在我正在阅读的伟大的书籍NodeJs design patterns我看到下面的例子: var fs = require('fs'); var cache = {}; function inconsistentRead(filename, callback) { if (cache[filename]) { //invoked synchronously callback(cache[filename]); } else { //asynchronous function fs.readFile(filename, 'utf8', function(err, data) { cache[filename] = data; callback(data); }); } } 然后: function createFileReader(filename) { var listeners = []; inconsistentRead(filename, function(value) { listeners.forEach(function(listener) { listener(value); }); }); return […]

了解Node.JS使用async.waterfall如何执行外部函数

我需要使用async.js模块执行asynchronous函数。 但是当我执行外部函数时,我遇到了一些问题。 代码通过。 但是当我更改全局variables为本地variables,我不能设置使用参数。 var async = require('async'); var ogs = require('open-graph-scraper'); // global variables var param1 = {url: 'http://www.google.com/'}; var param2 = {url: 'https://www.yahoo.com/'}; function function1(callback){ ogs(param1, function(error, data1) { callback(null, data1); }); } function function2(data1, callback){ ogs(param2, function(error, data2) { callback(null, data1, data2); }); } function function3(data1, data2, callback){ console.log(data1); console.log("—————"); console.log(data2); } […]

ExpressJS – 如何处理同时发生的请求? 请求似乎阻止彼此。

我有以下一段代码 var express = require('express'); var routes = require('./routes'); var http = require('http'); … app.get('/a',function(){ Card.findCards(function(err, result){ //Mongoose schema res.send(result); //Executes a query with 9000 records }) }); app.get('/b', function(req, res){ res.send("Hello World"); }); 我发现当我在localhost / a上获取时,大约需要2.3秒才能完成。 这并不奇怪,因为它从数据库中获取了相当多的数据。 不过,我发现如果我/ B正在加载时,B不会显示。 就好像呼叫/ a阻止对/ b的呼叫一样。 这是expression如何工作? 我总是假设个别路由是asynchronous的,因为他们采取callback,但似乎是明文一次只能处理一个请求。 直到res.end()被调用,没有其他请求得到处理。 我是否缺less我需要的configuration? 作为参考,这是我如何连接到mongoose mongoose.connect(dbConnectionString, {server:{poolSize:25}}); 这是我的http服务器初始化部分 http.globalAent.maxSockets = 20; // […]

调用node.js中的callback函数

我习惯于在Java中思考,我试图让我的头绕过node.js。 当程序出错时,我的程序需要logging信息,而且我发现我不得不在node.js程序中join很多样板代码,以便在Java中免费获得。 我的问题归结为: 有没有一种更简单/非模板化的方式来获取一系列callback中的类似堆栈的信息? 和/或 我是否无法正确掌握node.js,并试图强制asynchronousnode.js更像是同步Java? Java示例 这是一个尝试(并失败)连接到Mongo数据库的noddy Java程序:import java.net.UnknownHostException; import com.mongodb.Mongo; public class Test { public static void main(final String[] args) throws UnknownHostException { final Mongo mongo = a(); } private static Mongo a() throws UnknownHostException { return b(); } private static Mongo b() throws UnknownHostException { return c(); } private static Mongo c() […]

NodeJS 7:如何在Async Mongoose Promises中显示正确的堆栈跟踪

当我在与bluebird mongoose承诺错误。 我没有正确的行号,这使我很难find错误。 unknownFunction("A")#show me the correct line number in the trace async.timesSeries 100, (index, next) -> unknownFunction("B") #show me only the line number where I catch the error process.on 'uncaughtException', (err)-> console.log err.stack console.trace err throw err 问题:如何获得正确的行号而不是错误被捕获的行号? PS:我发现并尝试这个到目前为止: https : //github.com/groundwater/node-stackup但它给了我很多不相关的行号。 编辑: 这是我如何初步与蓝鸟mongoose: Promise = require("bluebird") Promise.config({ longStackTraces: true warnings: { wForgottenReturn: false […]

mongoose复杂(asynchronous)虚拟

我有两个mongoose模式如下: var playerSchema = new mongoose.Schema({ name: String, team_id: mongoose.Schema.Types.ObjectId }); Players = mongoose.model('Players', playerSchema); var teamSchema = new mongoose.Schema({ name: String }); Teams = mongoose.model('Teams', teamSchema); 当我查询队伍时,我也会得到虚拟生成的队伍 : Teams.find({}, function(err, teams) { JSON.stringify(teams); /* => [{ name: 'team-1', squad: [{ name: 'player-1' } , …] }, …] */ }); 但我不能 使用虚拟 ,因为我需要一个asynchronous调用: teamSchema.virtual('squad').get(function() { […]