bluebird

Flattening a Promise map

谁说我不能喝 提交于 2019-12-10 14:09:12
问题 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

Promise fulfillment handler undefined

最后都变了- 提交于 2019-12-10 13:04:34
问题 Very basic, it seems, implementation of a Promise based implementation is not returning the value/data I expect to see. This is how I am expecting this interface to work: sdk.request(options) => Promise~Response → Object (JSON) Here is the code in my model: return sdk.request(options).then(function (value) { return value; }); When I log the return of the model, I see this: { _bitField: 0, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined, _progressHandler0: undefined, _promise0:

What is wrong with this code that promisify a function?

℡╲_俬逩灬. 提交于 2019-12-10 12:19:28
问题 This function does not return a promise. It uses the csvtojson module. https://www.npmjs.com/package/csvtojson var CSVConverter=require("csvtojson").Converter; function get_json(cvs_file_location) { var data=fs.readFileSync(cvs_file_location).toString(); var csvConverter=new CSVConverter(); csvConverter.fromString(data,function(err,jsonObj){ if (err){ console.log("error msg: " + err); return null; } var json_csv = clone_obj(jsonObj); console.log(json_csv); return json_csv; }); } I would like

Chaining Requests using BlueBird/ Request-Promise

隐身守侯 提交于 2019-12-10 11:44:01
问题 I'm using the request-promise module and have found no mention of how to chain requests. I'm currently following their syntax of: request({options}) .then(function(result){...}) .catch(function(error){...}) However I want to be able to use Promise.all and try to make multiple calls at the same time and wait for them to all resolve and then proceed with other calls. For example I want to: Make a call to one app creating a User. AT THE SAME TIME, make a call creating an Address. Promise.all(

What to use instead of Promise.all() when you want all results regardless of any rejections

混江龙づ霸主 提交于 2019-12-10 11:23:53
问题 I'm using the Bluebird promise library in a node.js project. I have two operations that both return promises and I want to know when both are done, whether resolved or rejected and I need the return values from both. I'm reading the contents of multiple files and some of the files may not exist and that's an OK condition, so though fs.readFileAsync() will fail if the file doesn't exist, I still need the results of the other read operation. Promise.all(p1, p2) will reject if either p1 or p2

Meteor with Promises

◇◆丶佛笑我妖孽 提交于 2019-12-10 10:18:49
问题 I've been trying to get into the habit of using promises but ran into problems while trying to use them in server side code in the context of Meteor. This is the problem: if (Meteor.isServer) { Meteor.startup(function () { // code to run on server at startup p = function(){ return new Promise(function(res,rej) { res("asd"); }); }; p().then(function(asd){ console.log("asd is " + asd); return "zxc" }).then(Meteor.bindEnvironment(function(zxc){ console.log("zxc is " + zxc); return "qwe" })).then

Caching and pre-fetching expiring promises in Javascript

给你一囗甜甜゛ 提交于 2019-12-10 04:04:53
问题 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

Wrapping Node.js callbacks in Promises using Bluebird

拥有回忆 提交于 2019-12-10 03:09:21
问题 How do I wrap a Node.js callback using a Promise in Bluebird? This is what I came up with, but wanted to know if there is a better way: return new Promise(function(onFulfilled, onRejected) { nodeCall(function(err, res) { if (err) { onRejected(err); } onFulfilled(res); }); }); Is there a cleaner way to do this if only an error needs to be returned back? Edit I tried to use Promise.promisifyAll(), but the result is not being propagated to the then clause. My specific example is shown below. I

Mongoose with Bluebird promisifyAll - saveAsync on model object results in an Array as the resolved promise value

烈酒焚心 提交于 2019-12-10 02:41:58
问题 I'm using bluebird's promisifyAll with mongoose. When I call saveAsync (the promisified version of save) on a model object, the resolved value of the completed promise is an array with two elements. The first is my saved model object, the second is the integer 1 . Not sure what's going on here. Below is example code to reproduce the issue. var mongoose = require("mongoose"); var Promise = require("bluebird"); Promise.promisifyAll(mongoose); var PersonSchema = mongoose.Schema({ 'name': String

How do you create a global error handler for an es2017 javascript async/await function?

て烟熏妆下的殇ゞ 提交于 2019-12-09 17:58:45
问题 Bluebird 3.4.1 for promises, Chrome 56, Babel 6.23.1 Given: async login() { try { let response = await this.authservice.login('invalid-credentials'); } catch (error) { } } The above code simulates a 401 response error with a json object with the specific error message returned from the server which does trigger the catch statement. But instead of having to handle the error for each catch statement I would like to have a global error handler. In non async functions I use the window.onerror