Tag: asynchronous

尝试使用async.forEach Serial但失败 – Node.js

forEach,我正在使用asynchronous库。 在foreach循环中,我将调用不同的服务并获取数据。 这些数据应该是为了处理它在我看来,因为这些是一些着装细节,需要有序。 所以我试图通过添加外部服务callback内的done()使foreach循环同步。 var address_deails = []; var x = 0; async.forEach(lat_long, function (item, done) { external_servie(item,config,function (err, data) { if (err) { callback(err, null); } else { //code here ……. address_deails[x] = //some_data; } x++; done(); <————- Here }); }, function(err) { if(err) { callback(err,null) } else { callback(err,address_deails) } }); 但我每次都以不同的顺序获得价值。 我想按照0,1,2顺序获取数组键。 […]

混合asynchronous操作(callback和承诺)完成后如何执行任务?

我在写一些nodejs应用程序。 我的应用程序有一些npm模块的依赖。 在一个任务中,涉及两个asynchronous操作,两个asynchronous操作是以callback和承诺的forms。 下面我把一些示例代码: // the task which should be performed after async operation var myTask = function(){ // do something } // first async operation in the form of callback cbapi(myTask) // second async operation in the form of promise promiseapi() .then(myTask) 在上例中,当第一次asynchronous和第二次asynchronous操作完成时, myTask将执行两次。 但是,我想要的是只有在两个asynchronous操作完成后才执行一次。 有没有办法做到这一点?

node.async parallel:我们可以创builddynamic文本来表示任务吗?

在节点asynchronous方法中,我们将任务定义为“one”或“two”,如下例所示。 有没有办法使文本dynamic? var async = require("async"); async.parallel({ one: function(callback){ setTimeout(function(){ callback(null, 1); }, 200); }, two: function(callback){ setTimeout(function(){ callback(null, 2); }, 100); } }, function(err, results) { console.log(results); // results is now equals to: {one: 1, two: 2} });

创build多个dynamic对象NodeJs查询Mongodb

我有一个对象的数组 [ { userId: '541c83d89ff44f767a23c546', emailArray: [ 'abc@amazon.com', 'zxc@amazon.com', 'pqr@amazon.com', 'lol@amazon.com' ] }, { userId: '56bd6bf220591a124001d178', emailArray:[ 'mno@gmail.com', 'uvw@gmail.com' 'second@gmail.com' ] } ] 并为每个userId我想创build像下面的MongoDb查询对象 var q1 = { $and: [ {userId:userId1}, {"interactions.emailId": {$in:emailArray1} var q2 = { $and: [ {userId:userId2}, {"interactions.emailId": {$in:emailArray2} var q3 = { $and: [ {userId:userId3}, {"interactions.emailId": {$in:emailArray3} ….. var qn = […]

Node.jsasynchronous数组迭代

我想同步使用async.each遍历数组: async.each(supplier_array, function(supplier) { console.log('looking at : ' + supplier); knex(host_name + '.order_item').where({ supplier: supplier, order_id: order_id }).then(function(data) { console.log(data); knex(host_name + '.distributor').select() .then(function(data) { console.log(data); }).catch(function(error) { console.log('error: ' + error); }); }).catch(function(error) { console.log('error: ' + error); }); }); 我的supplier_array有3个元素。 那么应该发生什么事情呢(同步): 对于供应商1 /第一个ARR /第一个数组元素: 执行console.log(供应商) 执行console.log(order_item) 的console.log(分配器) 对于供应商2 /秒的数组元素: 执行console.log(供应商) 执行console.log(order_item) 的console.log(分配器) 对于供应商3 […]

悬挂callback:在每个callback已经返回之前返回响应

问题:你认为悬挂callback是不好的node.js风格,甚至是危险的吗? 如果是这样的前提下呢? 案例:如下所述,假设您需要在更新某些数据的快速服务器上调用数据库。 然而,客户不需要被告知结果。 在这种情况下,您可以立即返回响应, 而不是等待asynchronous调用完成。 这将被描述为没有更好的名字的悬挂callback 。 为什么这很有趣?:因为在大多数情况下教程和文档都显示等待的情况,在最糟糕的情况下教导callback地狱。 回想一下你说的快递,蒙古包和护照的第一次经历。 例: 'use strict' const assert = require('assert') const express = require('express') const app = express() function longOperation (value, cb) { // might fail and: return cb(err) …here setTimeout(() => { // after some time invokes the callback return cb(null, value) }, 4000) } app.get('/ping', function […]

承诺如何在这种情况下实施?

我有一个同步使用fs.readfile的问题,他们不inenterizing第一次,如果我再次请求,然后我有结果。 我知道我可以使用承诺,我发现从Kriskowal Q有用。 我尝试过,但没有成功。 我不太清楚如何实现它。 如果有人可以帮助,我将永远感激。 码: "use strict" var Q = require('q'); var fs = require('fs'); var arrayZipBand = []; var jsonZipCode = []; var arrayParsedZipcodeBr = []; exports.parse = function(opts) { if (opts.zipBand.constructor != Array) { opts.zipBand = [opts.zipBand]; } if (opts.location.constructor != Array) { opts.location = [opts.location]; } if (opts.config === 0) […]

在Grunt中显示输出为命令运行?

在这里,我没有输出,命令永远运行: grunt.registerTask('serve', function () { var done = this.async(); grunt.util.spawn({ cmd: 'jekyll', args: ['serve'], stdio: 'inherit' }, function (err, res, exit_code) { res.stdout && console.info(res.stdout); exit_code && console.error(res.stderr); done(err); }); }); 我想输出(和命令运行,直到错误或中断)。

Node.js – asynchronous方法调用问题

我正在使用node.js构build一个应用程序,并希望确认一个有关模块和节点asynchronous性质的问题。 如果我有一个模块,如: var email_list = function() { //perform some database query or calculation } exports.email_to = function() { email_list.forEach(function(email) { console.log(email); }); }; 而在另一个文件中,我打电话给我的email_to方法,在我的email_to函数被调用之前函数email_list被调用了吗? 如果没有,有没有办法保证email_to函数在email_list函数email_list调用? 在此先感谢〜

如何在NodeJs中手动更新多个Mongo文档?

我有一个在MongoDB中的users集合。 我需要为每个用户生成一个令牌并将其保存到数据库中: var crypto = require('crypto'); db.users.find({}).exec(function(users){ users.forEach(function(user){ user.token = crypto.randomBytes(32).toString('hex'); user.save(); }); }); 我总是对asynchronous方法感到困惑,只是无法理解它们是如何工作的…所以这段代码在save()调用完成之前就不存在了。 你将如何使它工作? 你将如何等待所有的save() call并打印Done! 控制台? 谢谢!