q

Returning an Array using Firebase

Deadly 提交于 2019-12-19 10:54:27
问题 Trying to find the best-use example of returning an array of data in Node.js with Q library (or any similar library, I'm not partial) when using Firebase .on("child_added") ; I've tried using Q.all() but it never seems to wait for the promises to fill before returning. This is my current example: function getIndex() { var deferred = q.defer(); deferred.resolve(new FirebaseIndex( Firebase.child('users').child(user.app_user_id).child('posts'), Firebase.child('posts') ) ); return deferred

How to populate Angular UI Bootstrap Typeahead with newest $resource

跟風遠走 提交于 2019-12-19 03:08:32
问题 According to this Paweł Kozłowski's answer, Typeahead from AngularUI-Bootstrap should work when asynchronously obtaining popup items with $resource in newest Angular versions (I'm using 1.2.X). Plunk - Paweł's version - Typeahead with $http I guess I don't know how to use it properly (As a result I get an error in typeaheadHighlight directive's code - typeahead treats instantly returned Resource s as strings and tires to highlight them). Plunk - Typeahead with $resource I think the critical

Q promise: are callbacks invoked in the same order as registered?

别等时光非礼了梦想. 提交于 2019-12-18 19:14:09
问题 I'm using the Q promise library. My code relies on the fact that the callbacks for a single promise are executed in the same order as they were registered. http://jsfiddle.net/HgYtK/1/ var deferred = Q.defer(); var promise = deferred.promise; ['first', 'second', 'third'].forEach(function (position) { promise.then(function () { alert(position); }); }); deferred.resolve(); This does produce the correct result, but I don't know if it's part of the spec or a happy coincidence that could break

Stop memory leaks with recursive promises

两盒软妹~` 提交于 2019-12-18 14:53:36
问题 How do I create a recursive chain of JavaScript Promise s with the Q library? The following code fails to complete in Chrome: <html> <script src="q.js" type="text/javascript"></script> <script type="text/javascript"> //Don't keep track of a promises stack for debugging //Reduces memory usage when recursing promises Q.longStackJumpLimit = 0; function do_stuff(count) { if (count==1000000) { return; } if (count%10000 == 0){ console.log( count ); } return Q.delay(1).then(function() { return do

Stop memory leaks with recursive promises

孤者浪人 提交于 2019-12-18 14:53:11
问题 How do I create a recursive chain of JavaScript Promise s with the Q library? The following code fails to complete in Chrome: <html> <script src="q.js" type="text/javascript"></script> <script type="text/javascript"> //Don't keep track of a promises stack for debugging //Reduces memory usage when recursing promises Q.longStackJumpLimit = 0; function do_stuff(count) { if (count==1000000) { return; } if (count%10000 == 0){ console.log( count ); } return Q.delay(1).then(function() { return do

Queuing promises

谁都会走 提交于 2019-12-18 13:29:12
问题 I use mbostock/queue for queuing few async operation. It is more to rate limit (UI generate few events, where the backend can process it slowly), and also to make sure they are processed sequentially. I use it like function request(d, cb) { //some async oper add.then(function(){ cb(null, "finished ") }) } var addQ = queue(1); addQ.defer(request) //called by few req at higher rates generated by UI I already uses angular.js $q for async operation. So, do I have to use mbostock/queue , or can I

Queuing promises

空扰寡人 提交于 2019-12-18 13:27:59
问题 I use mbostock/queue for queuing few async operation. It is more to rate limit (UI generate few events, where the backend can process it slowly), and also to make sure they are processed sequentially. I use it like function request(d, cb) { //some async oper add.then(function(){ cb(null, "finished ") }) } var addQ = queue(1); addQ.defer(request) //called by few req at higher rates generated by UI I already uses angular.js $q for async operation. So, do I have to use mbostock/queue , or can I

How do Promises/A+ implementations vary?

不打扰是莪最后的温柔 提交于 2019-12-18 07:41:07
问题 What aspects of a promise library does the spec not cover? What kind of things vary between implementations? Please illustrate with examples of actual differences (eg between Bluebird and Q). 回答1: Almost everything. The Promises/A+ spec is aimed for promise interoperability, it's built so promise libraries (and now, native promises) can talk to each other . The idea is for it to be possible to predict how a promise behaves and to define how promises are assimilated by other libraries. Quoting

How to maintain a promise-like API in this case?

不问归期 提交于 2019-12-17 21:19:15
问题 function foo(options) { if(!isValid(options)) { // I want to return a resolved promise here to permit client code to continue without a failure } return promisifiedThirdPartyApi(options); // Does not handle an invalid options object successfully } How can I idiomatically return a resolved promise in the "invalid" case? 回答1: Native Promises Take a look at the native Promise object's static methods resolve and reject. function foo(options) { if(!isValid(options)) { return Promise.resolve(); }

How do you properly return multiple values from a promise?

非 Y 不嫁゛ 提交于 2019-12-17 17:36:23
问题 I've recently run into a certain situation a couple of times, which I didn't know how to solve properly. Assume the following code: somethingAsync() .then( afterSomething ) .then( afterSomethingElse ) function afterSomething( amazingData ) { return processAsync( amazingData ); } function afterSomethingElse( processedData ) { } Now a situation might arise where I would want to have access to amazingData in afterSomethingElse . One obvious solution would be to return an array or a hash from