bluebird

Sequelize one to many assocation in multiple files

回眸只為那壹抹淺笑 提交于 2019-12-04 10:55:16
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: 'Country'}); Promise.promisifyAll(City); module.exports = City; And a second file country.js: const

How to reject (and properly use) Promises?

爷,独闯天下 提交于 2019-12-04 10:36:32
问题 Short story : Talking about Promises/A+ , what is the proper way to reject a promise - throwing an error? But if I miss the catch - my whole app will blow! How to use promisify and what are the benefits of it (maybe you'll need to read the longer version)? Is .then(success, fail) really an anti-pattern and should I always use .then(success).catch(error) ? Longer version , described as real life problem (would love someone to read): I have a library that uses Bluebird (A+ Promise

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

徘徊边缘 提交于 2019-12-04 06:37:49
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 event which works as expected. However this does not work for async functions. I have also added events

Combining promises and chaining

蹲街弑〆低调 提交于 2019-12-04 05:43:57
问题 Is there a good design pattern for utilizing promises, while still supporting chaining? For example, let's say that we have something like: 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(); If instead we change the methods to use promises, e.g. function Foobar() { this.foo = function() { var self = this; return new Promise(function (resolve, reject) {

Simple schdule nodejs script insert records twice while connecting to remote database

早过忘川 提交于 2019-12-04 05:38:30
问题 I have a schedule nodejs script. Basically, it has schedule and command tables. Schedule table have many command row. The nodejs script checks the schedule table every 5 seconds. If it is the scheduled time matching up current time. I insert the command row (from schedule table) to command table. The bug is I run my script on my laptop and testing local copy of the database on my laptop. A single command in schedule table is only inserted once . I run my script on my laptop and testing RDS

Under what circumstances might bluebird's “Possibly unhandled Error” warning be wrong?

。_饼干妹妹 提交于 2019-12-04 05:18:42
问题 The word "possibly" suggests there are some circumstances where you can get this warning in the console even if you catch the error yourself. What are those circumstances? 回答1: This is pretty well explained in the docs: Unhandled rejections/exceptions don't really have a good agreed-on asynchronous correspondence. The problem is that it is impossible to predict the future and know if a rejected promise will eventually be handled. The [approach that bluebird takes to solve this problem], is to

bluebirdjs promises wrapped inside a for loop

坚强是说给别人听的谎言 提交于 2019-12-04 05:08:01
问题 I have a bunch of functions used to provide data to my service. I want to loop through each of them and stop as soon as one of them returns the desired result. If the first one works, thats fine. If it has an exception or data is not valid, I would like to move to the next one and so on. How may I achieve this? I have the below code: handleData: function(address) { var self = this; return new Promise(function (resolve, reject) { for (var i = 0; i < self.listAllAvailableProviders.length; ++i)

nested promises in bluebird

怎甘沉沦 提交于 2019-12-04 03:20:43
问题 I'm trying to figure out how to use promises correctly with the bluebird library. I've come across some nested promises in my code and I noticed that in the bluebird docs it reads: if you are utilizing the full bluebird API offering, you will almost never need to resort to nesting promises in the first place. There are many other blog posts about promises being misused and nesting is a regular anti-pattern. loadCar(someUri) // jqXHR .then(function (car) { if (carHasFourDoors(car)) { loadMake

Using a custom promise as a generic type

倖福魔咒の 提交于 2019-12-04 02:56:05
I have an ambient TypeScript module represent a library that supports any Promises/A+ library: interface Test { funcName():Promise<string>; } So I need to adjust it in such a way as to make the protocol of any promise library accessible on declarative level: interface Test<P> { funcName():P<string>; } But the TypeScript immediately complains: Type 'P' is not generic , before I even use it. Note that I cannot include a custom promise library into the same file as Test , because I have to pass it in from another module. And if I change the code to this: interface AnyPromise<T, P extends Promise

Bluebird Promises Join behaviour

我是研究僧i 提交于 2019-12-04 01:57:05
If I execute the following code with Node.js var Promise = require('bluebird'); Promise.join( function A() { console.log("A"); }, function B() { console.log("B"); } ).done( function done() { console.log("done");} ); The console will log B done However I would expect A B done or B A done If it set a break point in function A it is never reached. Why is it that it processes B but not A? Benjamin Gruenbaum Promise.join takes promises as all its arguments but its last one, which is a function. Promise.join(Promise.delay(100), request("http://...com"), function(_, res){ // 100 ms have passed and