bluebird

Bluebird — a promise was created in a handler but was not returned from it

时光毁灭记忆、已成空白 提交于 2019-12-06 06:26:35
First of all, I know that I have to return promises to avoid this warning. I've also tried returning null as suggested here in the docs . Consider this piece of code, I'm using it in Mongoose's pre-save hook, but I've experienced this warning in other places: var Story = mongoose.model('Story', StorySchema); StorySchema.pre('save', function(next) { var story = this; // Fetch all stories before save to automatically assign // some variable, avoiding conflict with other stories return Story.find().then(function(stories) { // Some code, then set story.somevar value story.somevar = somevar; return

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

霸气de小男生 提交于 2019-12-06 06:24:32
问题 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

Sequelize one to many assocation in multiple files

流过昼夜 提交于 2019-12-06 05:04:40
问题 I am working on one to many assocations with sequelize. Most tutorials and documentation shows examples when both models are defined in the same file. I currently have two files, first city.js: const Promise = require('bluebird'); var Country = require('./country'); var City = sequelize.define("City", { id: { type: DataTypes.INTEGER, field: 'id', primaryKey: true, autoIncrement: true },... }, { freezeTableName: true, timestamps: false }); City.belongsTo(Country, {foreignKey : 'countryId', as:

Promise version of a “while” loop?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 04:51:47
I've come to the realization that since promises in ECMAScript 6 allow for synchronous coding of asynchronous functions, for every promise-laden piece of code there's a synchronous corollary. For instance: var data = processData(JSON.parse(readFile(getFileName()))); Is the same as: var data = getFileName() .then(readFile) .then(JSON.parse) .then(processData); Now for my current use-case I want to write code to pull data from a massive public API. The API is paginated, so in a purely synchronous world I would write something like the following: var data = []; var offset = 0; var total = 10000;

How to use Bluebird with current TypeScript?

给你一囗甜甜゛ 提交于 2019-12-06 04:39:27
问题 I just don't get it. Once this was relatively easy, I downloaded the snippet from DefinitelyTyped, it assumed it was globally declared, I added the script and it worked. Now it seems that I have no other option that to use a complex package manager and asynchronous loading system, possibly with an optimizer for production. Ideally I just want some TypeScript code like this // maybe some import? Promise.resolve("foo").then(function(msg) { console.log(msg); } compile to some JavaScript like

Nested For Loops converted to Nested Promises

≯℡__Kan透↙ 提交于 2019-12-06 01:36:28
I'm running into a problem where my program ends only on one iteration of the nameList , and I'm not exactly sure where the illogical code lies. Given globally: var _ = require("underscore"); var nameList = ["Bob", "Susie"] var jsonDict = {} My complicated nesting starts here, but I'm not sure how to fix it so that it iterates through both the nameList and the numbers for-loop 1-10 : return new Promise((res, rej) => { var promises = []; return Promise.map(nameList, function(personName){ for (var j=1; j<=10; j++){ return promises.push(new Promise(function(resolve, reject) { params['page'] = j;

Using Redis SCAN in NODE

放肆的年华 提交于 2019-12-06 00:18:30
问题 I have Redis with a lot of keys in some format and I want to get keys that match some pattern and do some operations on them. I don't use KEYS method since it's not recommend in production. Using SCAN I'm wondering what is the best way to write it in code. I have to do something like a while loop but using promises, my current solution looks like this (code is simplified a little): 'use strict' const Promise = require('bluebird'); const config = require('./config'); const client = require('.

Why do I not get the warning “a promise was created in a handler but was not returned from it”?

倖福魔咒の 提交于 2019-12-05 22:42:09
Edit: to be more clear - I'm NOT getting warning in example below. I would like to know why and see example code where warning would be printed. In my Node.js project I'm using bluebird Promises library. I observed some cases where I'm getting following warning: Warning: a promise was created in a handler but was not returned from it I understand that the problem occurs when there is promise that is not connected to any promise chain. I'm trying to fully understand how this works and what exactly the problem is in mentioned case. For that purpose I took example from the documentation . Here is

Creating node library that support both node callbacks and promise with bluebird

别来无恙 提交于 2019-12-05 19:47:13
I am creating a node module and I want to be able to support both node callback and Promise APIs. The library that I hear the best things about (mainly that is it the fastest) is bluebird . So after reading some docs and looking at some other libraries that are using bluebird, I thought this would be the cleanest way to get a method to support both node callback and Promise APIs: this.isAllowed = function(role, resource, permission, callback) { var isAllowedAsync = bluebird.promisify(isAllowed); return isAllowedAsync(role, resource, permission).nodeify(callback); }; However with this code, the

Express.js and Bluebird - Handling the promise chain

落花浮王杯 提交于 2019-12-05 19:28:36
In a backend API I have a login route which should perform the following sequence of actions: Given an username and password, try to authenticate the user against an Active Directory. If authentication has failed reply with status 401. If success, continue. Look for an user with the given username in the database. If not found reply with status 403, otherwise continue. Find if the user document has some details like email, display name, etc (in case this is not the first time logging in). If yes reply with the user object, otherwise continue. Get user details from the Active Directory and