Tag: promise

处理错误后跳过承诺链

使用https://github.com/kriskowal/q库,我想知道是否有可能做这样的事情: // Module A function moduleA_exportedFunction() { return promiseReturningService().then(function(serviceResults) { if (serviceResults.areGood) { // We can continue with the rest of the promise chain } else { performVerySpecificErrorHandling(); // We want to skip the rest of the promise chain } }); } // Module B moduleA_exportedFunction() .then(moduleB_function) .then(moduleB_anotherFunction) .fail(function(reason) { // Handle the reason in […]

Q Promise库的.finally()和.done()之间有什么区别?

使用Nodejs Q promise库的.finally()和.done()语句有什么区别。 例如,这两者之间的区别是什么? Q(…) .then(…) .finally(); //or fin() Q(..) .then() .done();

Mongoose和promise:如何获得一个查询结果数组?

使用mongoose从db和Q中查询promise的结果,但发现很难绕过我的头,只是得到一个可用的用户列表。 目前我有这样的一些东西: var checkForPerson = function( person ) { people = mongoose.model('Person', Person) return people.findOne({"_id": person }, function(err, doc) { if (err) console.log(err) if (doc !== null) { return doc } else { console.log('no results') } }) } var promises = someArrayOfIds.map(checkForPerson); // this is where I would like to have an array of models […]

在返回从循环中调用的数据库查询检索到的数据中的问题

我在循环中做了多个mongoDB查询。 并希望将所有结果作为一个数据数组发送。但是,当我简单地使用返回来发送数据时,它只是简单地返回undefined,不要等待所有数据库请求的结果。 我也尝试使用q.moulde,但同样的问题。 码: var getPrayerInCat = function(data){ var result ; var finalData = []; if(data.length >0){ data.forEach(function(data2){ var id= data2.id; Prayer.find({prayerCat:id},function(err,prayer){ var deferred = Q.defer() if (err) { // … console.log('An error has occurred'); // res.send(err); result= finalData = err } else { if(!prayer){ // console.log(data2.id+'–0'); data2.prayersCount = 0; result = deferred.resolve(finalData.push(data2)) } else […]

我怎样才能限制Q promise的并发性?

我如何编写一个限制Q promise并发性的方法? 例如,我有一个方法spawnProcess 。 它返回一个Q的承诺。 我不想每次都产生超过5个进程,但是对于调用代码是透明的。 我需要实现的是带签名的函数 function limitConcurrency(promiseFactory, limit) 我可以打电话给我 spawnProcess = limitConcurrency(spawnProcess, 5); // use spawnProcess as usual 我已经开始编写我的版本了,但是我想知道是否有人可以简单的实现我可以检查的版本。

蓝鸟Promisfy.each,for循环和if语句?

现在,父对象循环( m < repliesIDsArray.length )在第一个findOne触发之前完成,所以这全部只循环通过respondIDsArray.asynchronous的最后一个元素。 这个代码集的promisified版本的正确语法是什么? 对promisification新来说,想知道如何启动这个promisify +循环通过数组+帐户的if语句.. 蓝鸟是必需的,而Promise.promisifyAll(require("mongoose")); 叫做。 for(var m=0; m<repliesIDsArray.length; m++){ objectID = repliesIDsArray[m]; Models.Message.findOne({ "_id": req.params.message_id}, function (err, doc) { if (doc) { // loop over doc.replies to find the index(index1) of objectID at replies[index]._id var index1; for(var i=0; i<doc.replies.length; i++){ if (doc.replies[i]._id == objectID) { index1 = i; break; } […]

删除嵌套的承诺

我不熟悉NodeJS中的使用请求和承诺的承诺和编写networking代码。 我想删除这些嵌套的承诺,并链接他们,但我不知道我会怎么做/是否是正确的路要走。 exports.viewFile = function(req, res) { var fileId = req.params.id; boxContentRequest('files/' + fileId + '/content', req.user.box.accessToken) .then(function(response) { boxViewerRequest('documents', {url: response.request.href}, 'POST') .then(function(response) { boxViewerRequest('sessions', {document_id: response.body.id}, 'POST') .then(function(response) { console.log(response); }); }); }); }; 这是请求代码: var baseContentURL = 'https://api.box.com/2.0/'; var baseViewerURL = 'https://view-api.box.com/1/'; function boxContentRequest(url, accessToken) { return new Promise(function (resolve, reject) { […]

基于Promise的节点的http框架?

节点框架通常通过(err, result)callback来工作。 Node是否有一个基于承诺的http框架,并且有一个健康的社区和积极的开发(例如express)?

恰当的方式来跳过Q Promise中的then函数

在我的代码中,基于特定的条件,我想跳过done函数,而不pipe所有的函数。 这个问题的原始版本是在编辑。 以下是我正在处理的实际问题。 抱歉给你带来不便 实际问题: 我在读文件并处理它。 如果文件的内容符合一定的条件,就必须对文件系统进行一系列的操作(比如读写几个文件),然后执行donefunction。 如果条件失败, 我必须跳过所有的一系列操作,我必须直接执行done函数 。 我在所有的函数中返回一个对象(让我们说result ), then在下一次我更新result并返回它。 那么,当所有这些都完成后,就会有累积的result 。 最后, done会处理result并打印出来。 所以,如果条件最初没有得到满足,那么只会打印result (这将是空的)。 Q() .then(readFile) .then(function (contents) { var processResult = process the contents; if (processResult) { return {}; } else { // How can I skip to `done` from here } }) .then(function (results) { // do some […]

如何在node.js中实际使用Q promise?

这可能是一个noob问题,但我是新的承诺,并试图找出如何在node.js中使用Q. 我看到教程开始于 promiseMeSomething() .then(function (value) {}, function (reason) {}); 但是我没有把握到底是从哪里来的。 我想这是从 var outputPromise = getInputPromise() .then(function (input) {}, function (reason) {}); 但getInputPromise()从哪里来? 我以前没有提到它。 我已经把它包含在我的项目中了 var Q = require('q'); // this is suppose, the async function I want to use promise for function async(cb) { setTimeout(function () { cb(); }, 5000); } async(function () { console.log('async […]