Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

JavaScript Promises and race conditions

As someone who usually promotes using promises, my suggestion is:

Do not use promises here

Promises represent one time events. They are an abstraction over values, one a promise changes its state it can no longer be changed. A promise starts off as pending and changes state once to either fulfilled or rejected.

You are facing a scenario where you have many users, each user joins and needs to add events, the users might 'go away', your scenario simply doesn't describe the same linear flow promises excel at. Promises are useful for a certain scenario - they are not for every concurrency problem. Using an event-emitter here is perfectly appropriate.

Your case (a user joining) does not really represent a resolved proxy operation. The code that "doesn't miss messages" is indeed more correct.

If you still choose to use promises here

There are a few things you can do:

  • You can use Q's progression events and add a progress handler in the creation phase. Note that Kris (Q's author) has called progression broken, and it is being removed in the next version of Q. I recommend against it.
  • You can wrap the message callback to only fire once a handler has been attached - accumulate items fired with a handler when it is created (in an array) and then trigger them all when the message handler is added (after you resolve, in a .then on the deferred you return.

Categories : Javascript

Related to : JavaScript Promises and race conditions
Q and promises chaining
How about just wrapping it inside same then method? var p = pull(target) .then(function(data) { return A(); }) .then(function(data) { var result = B(); if (!result) return C() return result; })

Categories : Node Js
Mongoose - chaining promises
The problem could be that: create method returns a promise and it doens't have method exec If you want to use then() in your custom method you'll have to return a promise, but you're returning a mongoose document: return user; This will always returns a promise, it allows you to use then() after your method (You will have to add mpromise module): userSchema.statics.findByFacebookIdOrCreate =

Categories : Javascript
Angularjs dealing with promises and models
There could be an argument to make one API call, which returns everything you need, but there are compelling reasons the other way to. There is no general rule. More details about combining API requests here: Assuming the API is as you have described, there could be advantages in responding to the first call to Surah before loading the Ayahs, bu

Categories : Javascript
Attach callback to multiple jquery promises
Not exactly sure if this is what you're trying to do but it sounds like what you need to do is create a master deferred object then pipe the array of deferred objects to it. Add your handlers to the objects in the array using .always() which fires whether the deferred fails or succeeds. When all of the child deferred objects are resolved the master deferred .done() will fire. Also $.when() can ha

Categories : Javascript
Dojo promise all -- but that waits for all promises to return
There are two alternatives for this: Settle Dojo promises offer no tools for managing this case explicitly ala' bluebird's Promise.settle or when's when.settle. So we pretty much have to create it ourselves: function settle(promises){ // assuming "dojo/promise/all" and "dojo/Deferred" var d = new Deferred(); var countdownLatch = promises.length; var results = []; promises.forEa

Categories : Javascript
Recently Add
Busy Indicator while page is loading
parent.document.getElementById and images path
Can not get bootstrap datetime picker to display properly
reading select box jquery issues
DC.JS Pie Chart + Select Deslect from external JavaScript function
Why would I ever need to cancelAnimationFrame()
Angular.ui alert doesn't close
HTML/JS How to position a rectangle relative to 'any' side of the parent canvas element
parse variable to xml
C# Dictionary equivalent in JavaScript
ExtJS Dashboard Panel Fit Panel Vertically
jQuery cross domain ajax call - Interpreted as script but transferred with MIME type text/xml
Reactjs map returns unexpected token }
Working with multiple pages in apache cordova and vs2013
Resize iframe to loaded content
Why 13596*0.1 is different than 13596/10?
AngularJS Validate all forms on application load
javascript open popup without menu/url/titlebar/scrollbar/anything
Can I use Razor syntax inside Javascript included in a ScriptBundle?
How to show a box for comment reply with id
parse name from user input when using jquery serialize
How to parse and load javascript object?
Marionette not navigating to the same url
Adding Google+ Sign in button to website
GRUNT - Install Grunt Packages?
how should I create the path of templateUrl property in Angular ui-router?
Update mysql database with jquery array using ajax
Run the same function on each item in a list / array
Get all ticks values (NVD3)
Button Click Event not Passing input String to MVC Controller Method
© Copyright 2017 Publishing Limited. All rights reserved.