q

AngularJS promise returning empty object

给你一囗甜甜゛ 提交于 2019-12-24 01:54:51
问题 Basically what I want to do, is to assign some model value from function call that resolves promise. Like so value = someFun() This is a service from which I call this function app.factory('SomeService', function($q) { return { someFun: function() { var d = $q.defer(); try { d.resolve("hi"); } catch (e) { d.reject(e); } return d.promise.then(function(text){ return text; }); } }; }); Here is the HTML code <div ng-init="value = 'yes'"> <pre>{{value |json}}</pre> </div> <button type="button" ng

Whats the best approach to use q promise in a loop? Waiting for chain to complete before iterating to next

余生颓废 提交于 2019-12-24 00:58:39
问题 I have the following situation: var ids = [120, 121, 122, 123, 124] function dummyPromise(pause) { var deferred = Q.defer(); setTimeout(function() { console.log(pause); deferred.resolve(pause); }, pause); return deferred.promise; } for(var i = 0; i < ids.length; i++) { dummyPromise(ids[i]) .then(dummyPromise) .then(dummyPromise) .then(dummyPromise) .then(dummyPromise) .done(function(){ console.log('done') }) } I want to wait for chains to complete before iterating to next. Whats the best way

Break Out of then promises in Angularjs

孤街醉人 提交于 2019-12-23 20:04:09
问题 I am trying to find a way to break out of a promise chain in AngularJS code. The obvious way was to return an object and then check is validity in every "then" function in the chain. I would like to find a more elegant way of breaking out of a then chain. 回答1: In angular, there is the $q service that can be injected in directives, controllers etc, that is a close implentation of Kris Kowal's Q. So inside of then function instead of returning a value or something else that would be chained to

How to (elegantly) interrupt Promises chain execution with Q

馋奶兔 提交于 2019-12-23 16:06:27
问题 I have a chain of promises that looks like this: module.exports.deleteCommunityFollower = function deleteCommunityFollower(req, res){ var communityId = req.params.userId; var followerId = req.session.passport.user.userId; var community = new user_model.User(communityId); community.getFollower(followerId) .then(function(data) { if(data.length === 0) { res.sendStatus(404); //no follower found, interrupt execution } else { return community.removeFollower(data[0]); //returns a promise } }) .then

nodejs readers/writers concurrency

我的梦境 提交于 2019-12-23 15:29:41
问题 Here's some simple code that demonstrates what I'm trying to do myVar = 1 reader = () -> getDataFromServer1().then -> # uses myVar and does stuff according to its value # returns promise writer = () -> getDataFromServer2().then -> # assigns to myVar # returns promise Q.all([reader(), reader(), reader(), writer(), writer(), writer()]).done -> console.log 'done' So I have multiple threads running at the same time. some of them change the value of myVar and some read the value and rely on it.

q.js : Is it possible to know if a promise has resolved/rejected or not

穿精又带淫゛_ 提交于 2019-12-23 07:11:04
问题 In my scenario I return a promise when I'm making a request. In the end I resolve/reject the deferred obj. I want to reuse the promise if it hasn't been resolved/rejected. Any info would be useful. 回答1: I got the answer by looking into q.js source. deferred.promise.inspect().state This will return the state of the promise. returns "fulfilled" if it was resolved or fulfilled returns "rejected" if it was rejected returns "pending" if it hasn't been resolved or rejected 回答2: You can use the

How to optimize this Node.js + q code to prevent callback hell?

别说谁变了你拦得住时间么 提交于 2019-12-23 04:55:22
问题 I am using Q to prevent callback hell but I have reached a part of my code that I don't know how to arrange: I am searching for scheduled messages to be delivered. For each of them, I try to send them one by one and if it could be sent, removes it from the database. The ugly part is to have a then() inside a for loop . This way I end up having nested promises instead of nested callbacks!!!! Suggestions? appLog.debug("Looking for scheduled messages"); var messages = messageService

Promise chains are not executed sequentially using Q promise library

蓝咒 提交于 2019-12-23 03:12:41
问题 I am performing multiple batched asynchronous operations in my code. Although operations within a batch should execute asynchronously batches should be executed synchronously one after another. Here is a jsfiddle I created. Look at the console as all output is there. And here is the code for convenience: asyncChain(10, 'FIRST CHAIN') .then(function () { asyncChain(10, 'SECOND CHAIN'); }) .then(function(){ asyncChain(10, 'THIRD CHAIN'); }); function asyncChain(n, msg) { var promiseChain = Q

Jasmine clock tick & Firefox: failing to trigger a Q.delay method

自古美人都是妖i 提交于 2019-12-22 20:02:52
问题 Lazy loading tests: I am trying to build a test for Jasmine to test a method that uses Q.delay. To go around the 10 seconds wait i'm using Jasmine's clock: jasmine.Clock.tick(10010); This works on Chrome but does not work on Firefox. I saw that the delay method of Q utilized setTimeout so I can't see any reason for the different behaviors. Any ideas why it fails on Firefox? 回答1: With jasmine 2.0 and Q at the v1 tag, I'm able to run this spec: describe("testing", function() { beforeEach

Jasmine clock tick & Firefox: failing to trigger a Q.delay method

℡╲_俬逩灬. 提交于 2019-12-22 20:00:00
问题 Lazy loading tests: I am trying to build a test for Jasmine to test a method that uses Q.delay. To go around the 10 seconds wait i'm using Jasmine's clock: jasmine.Clock.tick(10010); This works on Chrome but does not work on Firefox. I saw that the delay method of Q utilized setTimeout so I can't see any reason for the different behaviors. Any ideas why it fails on Firefox? 回答1: With jasmine 2.0 and Q at the v1 tag, I'm able to run this spec: describe("testing", function() { beforeEach