bluebird

Catching async errors from eval using domain

房东的猫 提交于 2019-12-24 11:41:21
问题 I'm trying to catch async errors with the npm eval module. It's very similar to the normal eval, except it utilizes node's vm module directly. I just came across node's domain module. It allows me to catch async errors that occur within _eval . However I checked the documentation and I can't find a done event for domain. How am I supposed to know when to resolve the promise? var code = [ "setTimeout(function () {", " throw new Error('async error sim')", "}, 1000)" ].join('\n') var domain =

BluebirdJS: Make a function run in parallel

≯℡__Kan透↙ 提交于 2019-12-24 07:35:01
问题 I'm relatively new to Promises in Javascript, but have recently fell in love with their elegance, particularly in the Bluebird library. This is probably a newbie question, but how could I convert a normally-synchronous function to run asynchronously? If I, for example, wanted to compute Math.random()*RANGE thirty times in-parallel, how exactly could I do that with promises (in either Q or Bluebird)? 回答1: First off, promises won't help you make code run in parallel. They are a tool for running

Node.js: promisifying callback library for 'yield' keyword

烈酒焚心 提交于 2019-12-24 04:47:08
问题 I'm trying to build a simple REST API with Koa.js. It uses ES6 generator functions, which I find much more pleasant than callbacks (they're just like C#'s async-await). The yield keyword expects a thenable (promise, thunk, generator). I'm using Bluebird's promisifyAll method to promisify callback libraries (request in my case), but I still keep getting error. Here are my code and the error: var koa = require('koa') , route = require('koa-route') , app = module.exports = koa() , Promise =

Bluebird-Queue Concurrency in Promise Queues not working as expected

巧了我就是萌 提交于 2019-12-24 03:30:01
问题 I am using a bluebird-queue via NodeJS to queue HTTP endpoints as tasks. Each task has a 3-level Promise dependancy that has to resolve before it is complete. One Task GET -> endpoint 1 // returns promise GET -> other endpoints in async // returns promise POST -> final endpoint // return promise I put 20,000 of these tasks into the bluebird-queue with queue.add() and then subsequently call queue.start() . All errors are caught and the handlers resolve the Promise so the task can complete. I

How to use Ramda Pipe function with a mix of promises and static callbacks?

萝らか妹 提交于 2019-12-24 01:51:21
问题 Based on the help of @ScottSauyet I have been able to create a function resolving static and promise based callbacks for an initial data object. Now I want to be able to pipe this data object through a series of callbacks, but run into trouble once I add multiple promises into the mix. Current setup // Libaries const R = require('ramda'); const fetch = require('node-fetch'); const Promise = require('bluebird'); // Input const data = { array: [['#', 'FirstName', 'LastName'], ['1', 'tim', 'foo'

Is there a way to use Bluebird's Promise.each concurrently?

陌路散爱 提交于 2019-12-24 01:19:26
问题 Bluebird has a nice function called Promise.map that lets you pass in an extra argument for the amount of concurrent operations. e.g. yield Promise.map arrayOfThings, coroutine (thing) -> newThing = yield thing.operate() database.set newThing , concurrency: 500 However, Promise.map will keep an array of whatever database.set newThing returns in memory for all of arrayOfThings . I'd rather not store all of that in memory as it bogs down my server. Optimally, I would want to replace Promise.map

nodejs sqlite3 db.run as a bluebird promise

时光怂恿深爱的人放手 提交于 2019-12-23 17:25:15
问题 I'm attempting to use sqlite3 in a express app. Basically, I get a rest request, based on the rest request, I query an external REST request. Between the response from the external request & the data passed in from the original REST request, I then do an update or insert into one of my sqlite3 tables. The problem I'm running into, is that in db.run(sqlStatement, paramArray, function(err)) , the function(err) is a callback where err is either an error, or a nil . In addition to that, IF the

How to use the bluebird concurrency option for the map function

只愿长相守 提交于 2019-12-23 06:58:09
问题 I am trying to use bluebird's map function with the built-in concurrency control. I want to retrieve a list of names, then make a number of POST requests for each name. For example, I want to make a request for each name for each day of the week. However, I need to throttle the number of concurrent POST requests because the intended server has rate limits. function getNames() { //Open mongodb connection //Get collection and array of names //return array of names in a promise } function

Why does the first method of promisifying work and not the second one?

和自甴很熟 提交于 2019-12-23 06:22:29
问题 This is a follow-up question to What is wrong with this code that promisify a function? Method 1 works; var Converter = require('csvtojson').Converter; Promise.promisifyAll(Converter.prototype); var converter = new Converter(); Method 2 does not work; var Converter = require('csvtojson').Converter; var converter = Promise.promisifyAll(Converter.prototype); Why does method 1 work and not method 2? 回答1: Promise.promisifyAll(obj) returns obj , therefore ... Promise.promisifyAll(Converter

Testing Restify Route Handler that contains Promise Code Block, using SinonJs and Mocha

我的未来我决定 提交于 2019-12-23 00:48:08
问题 I have a restify action code block below: function retriveAll(req, res, next) { db.user .find({where: {id: 1}) .then(function(user){ res.send(user); }) .catch(function(details){ res.send(details.message); }) .finally(function(){ next(); }); } I want to test this action specifically validating that res.send() was called within this code block . And later on validating the res.send() returned data. I'm using SinonJs and Mocha for testing framework. Here's a sample test code block for the method