Tag: asynchronous

Node.js:等待来自asynchronous调用的响应

我正在开发一个API,通过它我可以添加一个用户列表作为JIRA观察者(所以我正在和JIRa REST API交谈) 这是这样做的function: for (var i = 0; i < votes.totalValue; i++) { var voter = { user : votes.rawVoteData[i].user, value : votes.rawVoteData[i].value, email : votes.rawVoteData[i].email, fname : votes.rawVoteData[i].fname, lname : votes.rawVoteData[i].fname }; // Add these users as watchers to the Jira jira.jira.addWatcher(issueId, voter.user, function(err, result){ // TODO: Return to callback rep++; console.log('user='+voter.user); console.log(result); […]

Marko和Koajs:为什么我的asynchronous片段渲染不起作用?

我试图让渐进渲染在Marko和Koajs工作。 首先,我使用request-promise模块为JSON数据馈送做出承诺。 rp = require('request-promise') function getDataFeed() { var url = "http://api.dronestre.am/data"; var options = { url: url, headers: { 'User-Agent': 'request' }, }; rp(options).then(function(result) { var info = JSON.parse(result); return info.strike; }); }; 然后,我使用koa路由器和Marko加载一个模板,在这个模板中,数据以数据formsinput // router method to serve first visualization router.get('/first', function *() { let data = { strikes : getDataFeed() }; this.body […]

解除在Node JS中运行的任务的日志logging

保存一个新行开始2个asynchronous作业。 我试图debugging一个后台作业,因为他们通过一些共享的模型代码运行。 问题是,如果我添加日志logging来debugging任何共享代码,突然它纠结,看起来像: 15:36:42 worker.1 | Running SQL "select count() .." (job #1) 15:36:42 worker.1 | Running SQL "select * .." (job #2) 15:36:42 worker.1 | Got to part 2 of the code (job #1) 15:36:42 worker.1 | Got to part 3 of the code (job #1) 15:36:42 worker.1 | Got to part 2 of […]

定义节点函数的自定义callback

所以我有一个函数可以读取大约500个不同链接的一堆数据,并将这些数据保存到一个文件中,这个过程需要几秒钟的时间。 诀窍是,一旦抓取完成,我想开始操作这些数据,但是我不知道该怎么去做。 我知道,如果我正在做一个基本的http.get调用,我可以使用内置的callback函数,但这是我创build的自定义函数,所以当整个过程结束时如何做callback? 我读过,我可以定义一个callback传递给我的函数,并在我的函数结束调用,但似乎只是运行在一个不同的线程,而不是等待我的数据完成,然后再运行。 任何意见将apprecaited,希望这是有道理的。

产生asynchronous执行的JavaScript

我想用代码说明问题: var async1 = f1(params, cb); var async2 = f2(params, cb); 现在我可以通过瀑布asynchronous过程调用async1,如下所示: async.waterfall([ function(cb){ async1(params, cb); async2(params, cb); }, //default process function(result, cb){ //some code here and then cb(null, result); } ], done); 我的问题是我如何等待async1 donecallback,所以我可以调用async2 。 这两个任务都需要运行默认进程和完成callback。 如果有一种方法/devise模式,我可以运行async1和完成后,我可以在同一个瀑布asynchronous函数内运行async2 ? 这两个asynchronous函数都需要完成默认进程和完成callback。 谢谢。

Node.js:为什么要在error handling期间返回一个callback的结果?

新手到Node.js在这里。 我正在通过NodeSchool.io上的教程来学习Node.js,并且在一个我们学习模块的教程中,我们被要求写这样的代码: // Some code… function filteredLs(dir, ext, callback) { fs.readdir(dir, function(err, files) { if (err) return callback(err); // return statement necessary here… callback(null, withExtension(files, ext)); // …but not here }) } module.exports = filteredLs; 我的问题是,在像这样的例子中,为什么在处理错误时需要包含return语句,但是当它为空时可以省略? 无论如何,我不明白函数的返回值可能会有什么用处,因为它在完成工作之后才会发生。 为什么它有所作为?

节点JSasynchronous映射是同步或asynchronous调用

当我尝试使用像瀑布,MapSeries等节点jsasynchronous模块function,其中大多数本质上是asynchronous的。 但看起来像asynchronous映射不是asynchronous的。 例如,我试过这个: var async = require('async'); console.dir('This is the begining of the program'); async.mapSeries([6,7,8,9], justAddOne, asyncMapCompleted); function justAddOne(number, callback) { callback(null, ++number); } function asyncMapCompleted(error, result) { console.log("map completed. Error: ", error, " result: ", result); } async.map([1,2,3,4,5], justAddOne, asyncMapCompleted); console.dir('This is the end of the program'); 我得到了以下输出: 'This is the begining of the […]

nodejs Async:concurency worker在队列中再次推送相同的任务

我想知道在完成使用nodejs的asynchronous模块之后,无限期地再次在队列中推送新任务的最佳方式是什么? var q = async.queue(function (task, callback) { console.log('hello ' + task.name); doSomeFunction(task.name, function(cb){ callback(); }); }, 2); q.drain = function() { console.log('all items have been processed'); } // add some items to the queue for (var i in list) { q.push({name: i}, function (err) { console.log('finished task'); //***HERE I would like to push indefinitely […]

Express Async – 发送后无法设置标题,双重callback?

我有一个恼人的地狱问题与'错误:发送后无法设置标头。 在Express中。 这个代码最初使用的是Restify而不是Express,而我一直以来以这种或那种forms出现这个问题。 该代码向另一个API(已编辑)发出2个asynchronous请求,并将结果合并到单个JSON中,然后将其存储到MongoDB中。 任何洞察力将不胜感激,因为我尝试了所有我能想到的,不知道为什么相同的function将在Restify中起作用,而不是Express在适当的变化。 请不要评论厄运金字塔,我知道这不是理想的,但这不是重点;) app.post('/rest/test', jsonParser, /*keycloak.protect(),*/ function (req, res, next) { var requestObj = req.body; try { /* run async requests in parallel */ async.parallel([ function asyncReqV3(callback) { request.post(reqUrl1, option_v3, function(err, response, body) { if(err) { console.log(err); callback(true); return; } else { callback(false, body); } }); }, /* v4 async request */ […]

等待来自Async,Protractor,nodejs的响应

我正在尝试创build一个帮助器函数,这个函数是一个asynchronous调用,它是我的数据设置的一部分,用于protactortesting。 有没有一种方法可以等待函数的响应,然后继续进行testing,这是我正在做的。 所以基本上testing应该等到asynchronous调用lo loaddata()完成。 我已阅读有关承诺的使用,但无法成功实施。 "use strict"; describe('sample passing test spec', function() { describe('sample passing test suite', function() { loaddata(); it('sample passing test', function () { datall(); expect("1").toEqual("2"); }); }); }); loaddata() is basically making a socket connection function loaddata(){ var net = require('net'); var client = new net.Socket(); client.connect(20000, '127.0.0.1', function() { console.log('Connected'); client.write('Hello, […]