天道酬勤,学无止境

bluebird

Use ldapjs with bluebird promise

I posted something similar here: Use ldapjs with promise. Unfortunately, it is still unsolved. This time I tried bluebird and hopefully I can get some luck. // https://www.npmjs.com/package/ldapjs var Promise = require('bluebird'); var ldap = Promise.promisifyAll( require('ldapjs') ); var config = require('./config'); var print_r = require('print_r').print_r; var my_filter = "(&(objectCategory=person)(objectClass=user)" + "(cn=" + 'someone' + "))"; var ldap_username = config.ad.username; var ldap_password = config.ad.password; var ldap_url = config.ad.url; var ldap_dn_search = config.ad.dn

2021-06-15 15:59:26    分类:问答    javascript   node.js   promise   bluebird   ldapjs

Unhandled rejection error Bluebird

I have the following code. And it works as expected without throwing a unhandled rejection error. p = new Promise (fulfill, reject) -> reject new Error 'some error' p.catch (error) -> console.log error Now, the second code example does throw an unhandled rejection error. Can someone explain to me why this is happening when im clearly handling the error. p = new Promise (fulfill, reject) -> reject new Error 'some error' p.then -> console.log 'ok' p.catch (error) -> console.log error Btw. I'm testing in chrome and bluebird v3.4.7

2021-06-15 01:59:32    分类:问答    javascript   coffeescript   bluebird   es6-promise   unhandled-exception

Caching and pre-fetching expiring promises in Javascript

Promises are my preferred way of managing my asynchronous code in Javascript. Memoize (memoizee on npm) is a Javascript library for easily caching & pre-fetching results of functions. Ideally I want to combine the best of both, and have the ability to "expire" a Promise and pre-fetch a new Promise result (when the cache is touched and near to expiring). Memoize can do this, but it wasn't built with Promises in mind. (I understand that Promises have a built-in "forever-cache" as is their nature, but forever is too long for my application) My best attempt to do this so far is as follows (node.js

2021-06-14 17:18:41    分类:问答    javascript   node.js   promise   memoization   bluebird

Flattening a Promise map

I'm curious how you go about flattening the results from a Promise map of promises that are arrays. I have a function that Promise.maps over a set of values that they themselves are promises (needing to be resolved) and are returning an array. So, I get back something like: [ [1, 2, 3], [1, 2, 3], etc. ] I've been using the lodash/underscore ._flatten after, however, I'm sure there is a cleaner method. return Promise.map(list, function(item) { return new Promise(function(res, rej) { return res([1, 2, 3]); }); });

2021-06-14 08:10:16    分类:问答    promise   bluebird

结合承诺和链接(Combining promises and chaining)

问题 是否有一个很好的设计模式来利用承诺,同时仍然支持链接? 例如,假设我们有这样的东西: function Foobar() { this.foo = function() { console.log('foo'); return this; }; this.bar = function () { console.log('bar'); return this; }; } var foobar = new Foobar(); foobar.foo().bar(); 相反,如果我们更改方法以使用承诺,例如 function Foobar() { this.foo = function() { var self = this; return new Promise(function (resolve, reject) { console.log('foo'); resolve(self); }); }; ... } 有什么好方法可以做这样的事情: var foobar = new Foobar(); foobar.foo().bar().then(function () { // do something }); 回答1 相反,如果我们更改方法以使用承诺,例如this.foo = function() { var self = this; return new Promise

2021-06-12 19:19:44    分类:技术分享    javascript   promise   chaining   method-chaining   bluebird

蓝鸟承诺范围(Bluebird Promise Scope)

问题 我刚刚开始使用 promise 来清理一些“回调地狱”。 我决定尝试 bluebird 并且我正在浏览器中运行它,但立即遇到了范围界定问题。 有没有办法在新的 Promise 中设置 thisArg? 下面的示例显示 Promise 解析器中的“this”值设置为浏览器窗口,但我希望将其设置为周围的范围,以便我可以轻松访问成员变量。 我注意到有一个 .bind() 方法,但它只适用于 'then()' 方法,而不是 promise。 我也意识到我可以在承诺之前使用 'var me = this' 并使用闭包,但我想尽可能避免它。 function MyObject() { this.value = 7; } MyObject.prototype.getValue = function () { return new Promise(function (resolve) { // some request/processing that takes a long time var result = Ajax.request(...); resolve({ value: this.value, result: result }); // 'this' is set to window instead of the instance, // resulting in this

2021-06-12 11:51:09    分类:技术分享    javascript   promise   bluebird

在什么情况下蓝鸟的“可能未处理的错误”警告可能是错误的?(Under what circumstances might bluebird's “Possibly unhandled Error” warning be wrong?)

问题 “可能”一词表示在某些情况下,即使您自己发现了错误,您也可以在控制台中收到此警告。 那些情况是什么? 回答1 这在文档中得到了很好的解释: 未处理的拒绝/异常并没有真正达成一致的异步通信。 问题是无法预测未来并知道最终是否会处理被拒绝的承诺。 [bluebird 解决此问题的方法] 是,如果在第二轮开始时未处理拒绝,则调用已注册的处理程序。 默认处理程序是将堆栈跟踪写入浏览器中的stderr或console.error 。 这与同步代码发生的情况很接近 - 您的代码没有按预期工作,您打开控制台并查看堆栈跟踪。 好的。 当然,这并不完美,如果您的代码出于某种原因需要在 Promise 挂起一段时间后突然进入并将错误处理程序附加到某个 Promise 上,那么您将看到烦人的消息。 因此,例如,这可能会警告未处理的错误,即使它会得到很好的处理: var prom = Promise.reject("error"); setTimeout(function() { prom.catch(function(err) { console.log(err, "got handled"); }); }, 500);

2021-06-12 05:57:03    分类:技术分享    javascript   bluebird

我违背了我的诺言(I Broke My Promise)

问题 所以..我最难学习如何承诺。 我正在按照我的建议使用bluebird (https://github.com/petkaantonov/bluebird) - 为了驯服我一直遇到的回调地狱。 例子: function login(req,res,con,mysql,P) { var ref = undefined; con.getConnection(function(err,connection) { if (err) console.log("Get Connection Error.. "+err); con.query('SELECT password,id FROM player WHERE name='+mysql.escape(req.body.user),function(err,rows,fields) { if (err) throw err; if (!rows[0]) { res.send({ "msg":"Your username and or password was incorrect.", "flag":true, "title":": Login Failed" }); } if (rows[0].password !== "undefined") { if (hash.verify(req.body.pass,rows[0]

2021-06-11 18:03:04    分类:技术分享    javascript   node.js   promise   bluebird

使用 node.js、streams 和 promise 下载文件(Downloading files with node.js, streams, and promises)

问题 这是我的代码片段: var processListing = function (directoryItems) { console.log('foreach'); var itemsToDownload = []; directoryItems.forEach(function (element, index, array) { //Ignore directories if (element.type === 'd') { console.log('directory ' + element.name); return; } //Ignore non zips if (path.extname(element.name) !== '.zip') { console.log('ignoring ' + element.name); return; } //Download zip itemsToDownload.push({ source: element.name, destination: element.name }); //aftpSystem.downloadFile(element.name, element.name); }); console.log('after foreach'); return itemsToDownload; }; var

2021-06-11 17:21:10    分类:技术分享    javascript   node.js   stream   promise   bluebird

bluebird - fs.readdir().then - cannot read propery then of undefined

I am trying to promisify the fs readdir function using bluebird. I just want to read all the files in a directory, and then output the array with console.log. const Promise = require('bluebird'); const fs = Promise.promisifyAll(require('fs')); fs.readdir('./XML').then(function(err, directories) { console.log(directories); }); I get the following output: fs.readdir('./XML').then(function(err, directories) { ^ TypeError: Cannot read property 'then' of undefined at Object. (/Users/shooshte/Sportradar/notThatKindOfPeon/bluebird.js:4:20) at Module._compile (module.js:570:32) at Object.Module

2021-06-11 08:02:38    分类:问答    javascript   node.js   bluebird