Tag: 蓝鸟

如何使用蓝鸟承诺将parameter passing给护照策略callback?

我正在努力promisify passport.js的本地策略 。 我对诺言和护照都很陌生,而且我非常依赖这个评论线程 ,它使用蓝鸟的Promise库来传递额外的参数给passport的done()callback函数。 这个评论导致了一个新的蓝鸟实现处理额外的callback参数,但我不能让它在我自己的代码中工作: const passport = require('passport'); const User = require('../models/user'); const LocalStrategy = require('passport-local'); const NoMatchedUserError = require('../helpers/error_helper').NoMatchedUserError; const NotActivatedError = require('../helpers/error_helper').NotActivatedError; const localOptions = { usernameField: 'email' }; const localLogin = new LocalStrategy(localOptions, function(email, password, done) { let user; User.findOne({ email: email }).exec() .then((existingUser) => { if (!existingUser) { throw […]

蓝鸟过滤捕捉巴贝尔和扩展错误类

我运行一个nodejs(服务器)项目(v6.11.2),并通过扩展Error类定义了自定义错误。 我使用babel编译,我使用的babelconfiguration是: { "presets": [ ["env", { "targets": { "node": "current" } }] ] } AppError类: export default class AppError extends Error { constructor (message, status) { super(message); this.name = this.constructor.name; Error.captureStackTrace(this, this.constructor); this.status = status || 500; } } 我想检查如果取消方法抛出一个AppError。 当我尝试使用蓝鸟过滤的catch,我得到以下错误: 类构造函数AppError不能被调用没有“新” cancel() .catch(AppError, (e) => { // This is not working }); 但是下面的代码将会正常工作: […]

使用superagent-bluebird-promise处理节点服务器上的服务器redirect

我有一个坐在我的Java巨石(后端)和浏览器之间的节点服务器。 节点服务器处理反应组件和所有的服务器侧渲染。 我使用superagent-bluebird-promise在节点服务器上处理xhr。 现在,节点服务器也会访问后端的apis,后端可以在某些情况下将xx调用redirect到另一个URL。 在这里我想处理这个。 我不希望节点服务器打到redirect,但我想抓住,并根据一些条件做redirect。 有可能处理? 如果是的话,怎么样? 如果不是的话,在节点服务器上处理后端redirect的最好方法是什么?

嵌套while循环承诺

我已经按照正确的方式编写了许诺的循环。 为成功创build承诺循环。 但是,似乎这种方法不适用于嵌套循环 我想要模拟的循环: var c = 0; while(c < 6) { console.log(c); var d = 100; while(d > 95) { console.log(d); d–; } c++; } (请注意,我在这里简化了promFunc()的逻辑,所以不要认为它是无用的) : var Promise = require('bluebird'); var promiseWhile = Promise.method(function(condition, action) { if (!condition()) return; return action().then(promiseWhile.bind(null, condition, action)); }); var promFunc = function() { return new Promise(function(resolve, reject) […]

我怎样才能使用蓝鸟Promiseify节点的游标和/或集合'toArray()mongodb模块?

相关软件包: "dependencies": { "mongodb": "1.4.x", "bluebird": "2.3.x" } 我看过: 我如何使用蓝鸟promisify MongoDB原生的Javascript驱动程序? 蓝鸟Promisfy.each,for循环和if语句? https://stackoverflow.com/a/21733446/438992 蓝鸟的promisification文件 其他一些地方 我困在findAsync({}) 。 我更喜欢一个游标,但是很less有我想要调用toArray() 。 这也是可能的,我完全错了。 MongoClient.connectAsync('mongodb://127.0.0.1:27017/sr') .then(function(_db) { db = _db; return db.collectionAsync('posts'); }) .then(function(colPosts) { return colPosts.findAsync({}); }) .then ( A MIRACLE OCCURS ) .catch(function(e) { console.log(e); }) .finally(function() { if (db) db.close(); }); 在奇迹发生的地方,我想要迭代游标结果或数组集合。 我在解决如何解决这个问题方面遇到困难。

我应该完全消除从我使用的代码(Bluebird)承诺的try / catch?

在我调用promisified函数之前,我有几个非常基本的设置步骤,我正在考虑将它们包装在try / catch块中,因为这似乎是最简单的方法。 不过,对我来说似乎有点肮脏。 我应该做一个返回一个Promise的函数,即使它很简单吗? 这是一个例子。 try thingyId = req.params.id # here I am 99.999% sure that params is defined, # but if for some bizarre reason it's not, I'd like to handle that error # instead of breaking the whole program catch console.log "error: " + e # do normal promisified functions 或者我应该写这个 setThingyId […]

VersionError:仅在(Mocha)testing中发现匹配的文档错误

我已经阅读了其他问题/答案的人有版本的关键,但由于某种原因,我不明白为什么这具体发生在我的情况。 所以我有以下摩卡testing: it('should be able to save one job', function (done) { Promise.join(user.saveAsync(), company.saveAsync(), function (savedUser, savedCompany) { user = savedUser[0]; user.jobs.push(job); user.saveAsync() .spread(function (savedUserWithJob) { user = savedUserWithJob; user.jobs.should.have.length(1); done(); }) .catch(function (err) { done(err); }); }); }); 这一切都很好,并通过。 即使在运行时我也没有任何其他问题。 现在当我尝试在第一个之后再次运行相同的testing时, it('should be able to save one job', function (done) { ….. }); it('should […]

在Bluebird中返回被拒绝的promise和onPossiblyUnhandledRejection处理程序

我正在用这个模式编写很多代码,在Bluebird的文档中可以find类似的例子: var promise4 = Promise.join(promise1, promise2, function (value1, value2) { // promise 3 needs the values resolved from promise1 and promise2 return promise3(value1, value2); }); 我的error handling是这样完成的: Promise.all([ promise4 ]) .catch(function (err) { // handle error }); 问题是,如果promise3被拒绝,它会触发蓝鸟的onPossiblyUnhandledRejection处理程序,因为它被连接到任何承诺链(具有error handling)之前被拒绝。 当没有任何事情是错误的时候,这会导致大的错误信息被打印出来。 我不知道该怎么做,因为一方面,上面的模式对于构造并发逻辑非常有用,而且很容易理解,但是另一方面,我不想简单地覆盖onPossiblyUnhandledRejection,因为它可以如果我真的忘记处理一些事情 蓝鸟的文档types解决了这个问题,使得它听起来好像上面的模式是一个奇怪的: “如果你的代码由于某种原因需要突然冒出来,并附上error handling程序的承诺后一些承诺挂了一段时间,然后你会看到恼人的消息” 我的“某些原因”是我需要构build高度并发的软件。 这是我接近这种问题的根本缺陷吗? 如果没有,我应该如何解决这个问题,而不是完全删除这些警告?

NodeJS使用BlueBird来提升现有的API

我有一个非常简单的API,从服务器获取一些数据,做一些处理,然后将其发送到客户端。 我正在尝试“Promisify”这个小模块。 下面是一个例子 var MyModule = function (){} MyModule.prototype.makeRequest = function(requestURL){ //Set request up //Return the promise for the request. return request(requestOptions); } MyModule.prototype.myFunction = function(var1, var2, cb){ var URL = "http://….//"; this.makeRequest(URL) .then(function(data) { //Some processing logic cb(null,data); }) .catch(function(err) { cb(err,null); }) } module.exports = MyModule; 然后消耗这个模块,我想要做以下… var MyModule = new(require('../lib/MyModule')); MyModule.myFunction(var1,var2) .then(function(data) […]

地图/join蓝鸟和mongoose

我想映射和join我的Mongoose模型,例如: MyModel.myPromisifiedMethod parameter, (err,res) -> .then((res) -> # do stuff to res ) .then((res) -> Promise.map res, ((eachItem) -> # do stuff to to eachItem AnotherModel.get parameter, (err,res) -> # do stuff with res and eachItem ) join eachItem, ((eachItem) -> console.log eachItem ) ) .then((finalResult) -> res.status(202).send result:finalResult ) 地图是一个迭代器,我需要它,因为我对Mongo的请求长度是50000个文档。 但是上面的不工作,特别是连接部分。 我在网上找不到任何例子。 帮助表示赞赏。 这就是我进一步工作的方式,包含以下答案: […]