bluebird

Caching and pre-fetching expiring promises in Javascript

柔情痞子 提交于 2019-12-05 05:00:39
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

ES6 Promise / Typescript and the Bluebird Promise

感情迁移 提交于 2019-12-05 00:08:16
I have a nodejs / typescript 2 project and use the es6-promise package. Now i would like to get rid of the extra package because i can target ES6 in typescript directly. So i removed the es6-promise package and changed the tsconfig.json to target es6. { "compilerOptions": { "target": "es6", // ... } } Many 3rd party packages uses the Bluebird promise but the promise definition is incompatible to the default es6 promise as stated on different posts on github bluebird 3.0 definifion is not assignable to ES6 Promises Provide a way to load Bluebird globally in es6 compilation target. Add Symbol

How to bulk insert in mySql and node.js using mysljs

天大地大妈咪最大 提交于 2019-12-04 20:27:29
问题 Im not able to use bulk insert in my DB using node.js lib mysljs. I followed answers from: How do I do a bulk insert in mySQL using node.js with no success. var sql = "INSERT INTO resources (resource_container_id, name, title, extension, mime_type, size) VALUES ?"; var values = [ [1, 'pic1', 'title1', '.png', 'image/png', 500], [1, 'pic2', 'title2', '.png', 'image/png', 700]]; return connection.query(sql, [values], (result) => { if (err) throw err; connection.end(); }); I keep getting error:

How to promisify correctly JSON.parse method with bluebird

偶尔善良 提交于 2019-12-04 19:48:51
问题 I'm trying to promisify JSON.parse method but unfortunately without any luck. This is my attempt: Promise.promisify(JSON.parse, JSON)(data).then((result: any) => {... but I get the following error Unhandled rejection Error: object 回答1: Promise.promisify is thought for asynchronous functions that take a callback function. JSON.parse is no such function, so you cannot use promisify here. If you want to create a promise-returning function from a function that might throw synchronously, Promise

Testing rejected promise in Mocha/Chai

你离开我真会死。 提交于 2019-12-04 17:02:43
问题 I have a class that rejects a promise: Sync.prototype.doCall = function(verb, method, data) { var self = this; self.client = P.promisifyAll(new Client()); var res = this.queue.then(function() { return self.client.callAsync(verb, method, data) .then(function(res) { return; }) .catch(function(err) { // This is what gets called in my test return P.reject('Boo'); }); }); this.queue = res.delay(this.options.throttle * 1000); return res; }; Sync.prototype.sendNote = function(data) { var self = this

How promisifyAll works, or what are the requirements for it work?

送分小仙女□ 提交于 2019-12-04 16:51:17
问题 In a promise library bluebird have function promisifyAll or other similar libraries that claim to convert async functions with callback patterns into promise based ie. resolve() , reject() , or done() ..So how does it work? For example: function myAsync1 (data, url, callBack) {...} and if i put it in Promise.promisify(myAsycn1); then will my function work like this.. myAsync1('{..}', 'http://..').then(function(){...}); This is have been bothering me. Is there a pattern that async non promise

I Broke My Promise

丶灬走出姿态 提交于 2019-12-04 15:57:56
问题 So.. I'm having the hardest time learning how to Promise. I'm using bluebird (https://github.com/petkaantonov/bluebird) as suggested to me -- in order to tame my callback hell I've been getting. Example: 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 (

How to promisify a MySql function using bluebird?

℡╲_俬逩灬. 提交于 2019-12-04 14:25:58
问题 Some time ago I decided to switch from PHP to node. In my first projects I didn't want to use any ORM since I thought that I didn't need to complicate my life so much learning another thing (at the moment I was learning node and angular) therefor I decided to use mysql package without anything else. It is important to say that I have some complex queries and I didn't want to learn from sctratch how to make them work using one of the 9000 ORM node have, This is what I've been doing so far:

How to reuse promises?

醉酒当歌 提交于 2019-12-04 12:10:41
I am trying to reuse the the data returned from promise here. But, the problem is, after the first call to checkPromise function, it immediately calls the second function, and the promise for the first function is not fulfilled, so it never returns any data, and hence it never enters the if clause. How do I reuse a promise? var Promise = require('bluebird'); var request = Promise.promisify(require("request")); var url = 'http://www.google.com'; var obj = new Object; function apiCall(url) { return new Promise(function (resolve, reject) { request(url).spread(function(response, body) { return

Bluebird.js custom Error catch function, does not apply on the first promise?

心不动则不痛 提交于 2019-12-04 12:04:04
问题 I'm trying to use the custom error handlers of Bluebird.js. In the example bellow the catch-all handler is called, not the MyCustomError handler, but when I moved the rejection into the then function (and resolved the firstPromise...), the MyCustomError handler is called. Why is that? got something wrong? Thanks. var Promise = require('bluebird'), debug = require('debug')('main'); firstPromise() .then(function (value) { debug(value); }) .catch(MyCustomError, function (err) { debug('from