q

throw Error after promise is rejected - Q

泪湿孤枕 提交于 2019-12-01 08:45:13
问题 following is a short example for using a promise with Q. this is test1.js: function testDefer() { var deferred = Q.defer(); fs.readFile("foo.txt", "utf-8", function (error, text) { if (error) { deferred.reject(new Error(error)); } else { deferred.resolve(text); } }); return deferred.promise; } and this is test2.js (function(){ 'use strict'; var test1 = require('./test1'); test1.testDefer().then( function(data){ console.log('all good'); }, function(err) { //upon error i might want to throw an

How to Test Value Returned in Promise from AngularJS Controller with Jasmine?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 05:40:31
问题 I have a controller that expose a function that returns some text after a rest call. It works fine, but I'm having trouble testing it with Jasmine. The code inside the promise handler in the test never executes . The controller: /* global Q */ 'use strict'; angular.module('myModule', ['some.service']) .controller('MyCtrl', ['$scope', 'SomeSvc', function ($scope, SomeSvc) { $scope.getTheData = function (id) { var deferred = Q.defer(); var processedResult = ''; SomeSvc.getData(id) .then

Angular promise not resolving in jasmine

断了今生、忘了曾经 提交于 2019-12-01 04:42:28
I have the following jasmine test: it('should resolve promise', inject(function ($q, $rootScope) { function getPromise(){ var deferred = $q.defer(); setTimeout(function(){ deferred.resolve(true); }, 1000); return deferred.promise; } var p = getPromise(); var cb = jasmine.createSpy(); runs(function(){ expect(cb).not.toHaveBeenCalled(); p.then(cb); $rootScope.$apply(); }); waitsFor(function(){ return cb.callCount == 1; }); runs(function(){ expect(cb).toHaveBeenCalled(); $rootScope.$apply(); }); })); I thought $rootScope.$apply was supposed to resolve all outstanding promises, but somehow it does

How to resolve $q.all?

狂风中的少年 提交于 2019-12-01 03:49:39
I have 2 functions, both returning promises: var getToken = function() { var tokenDeferred = $q.defer(); socket.on('token', function(token) { tokenDeferred.resolve(token); }); //return promise return tokenDeferred.promise; } var getUserId = function() { var userIdDeferred = $q.defer(); userIdDeferred.resolve('someid'); return userIdDeferred.promise; } Now I have a list of topics that I would like to update as soon as these two promises get resolved var topics = { firstTopic: 'myApp.firstTopic.', secondTopic: 'myApp.secondTopic.', thirdTopic: 'myApp.thirdTopic.', fourthTopic: 'myApp.fourthTopic

What are the ways to display 'chunked' responses as soon as they come into AngularJS?

余生长醉 提交于 2019-12-01 03:44:54
Currently I have a problem displaying 'chunks' of responses that I am sending from my Web Service Node.js server (localhost:3000) to a simulated client running on a Node.js server (localhost:3001). edit * - Current implementation just uses Angular's %http as the transport without web-sockets The logic goes as follows: 1 . Create an array on the client side of 'Cities' and POST them (from the AngularJS controller) to the Web Service located at: localhost:3000/getMatrix $http({ method: 'POST', url: 'http://localhost:3000/getMatrix', data: cityArray }). success(function (data,status,headers

Javascript: How to iterate on array using promises?

早过忘川 提交于 2019-12-01 03:26:32
LIVE DEMO Given the following function: function isGood(number) { var defer = $q.defer(); $timeout(function() { if (<some condition on number>) { defer.resolve(); } else { defer.reject(); } }, 100); return defer.promise; } and an array of numbers (e.g. [3, 9, 17, 26, 89] ), I would like to find the first "good" number. I would like to be able to do this: var arr = [3, 9, 17, 26, 89]; findGoodNumber(arr).then(function(goodNumber) { console.log('Good number found: ' + goodNumber); }, function() { console.log('No good numbers found'); }); Here is one possible recursive version to implement this:

Angular promise not resolving in jasmine

梦想与她 提交于 2019-12-01 03:06:01
问题 I have the following jasmine test: it('should resolve promise', inject(function ($q, $rootScope) { function getPromise(){ var deferred = $q.defer(); setTimeout(function(){ deferred.resolve(true); }, 1000); return deferred.promise; } var p = getPromise(); var cb = jasmine.createSpy(); runs(function(){ expect(cb).not.toHaveBeenCalled(); p.then(cb); $rootScope.$apply(); }); waitsFor(function(){ return cb.callCount == 1; }); runs(function(){ expect(cb).toHaveBeenCalled(); $rootScope.$apply(); });

When to reject/resolve a promise

﹥>﹥吖頭↗ 提交于 2019-12-01 03:05:44
I am thinking about when exactly I need to reject a promise. I found a couple of questions regarding this topic, but could not find a proper answer. When should I reject a promise? This article http://howtonode.org/6666a4b74d7434144cff717c828be2c3953d46e7/promises says: Resolve: A successful Promise is 'resolved' which invokes the success listeners that are waiting and remembers the value that was resolved for future success listeners that are attached. Resolution correlates to a returned value. Reject: When an error condition is encountered, a Promise is 'rejected' which invokes the error

How to resolve $q.all?

若如初见. 提交于 2019-12-01 00:07:05
问题 I have 2 functions, both returning promises: var getToken = function() { var tokenDeferred = $q.defer(); socket.on('token', function(token) { tokenDeferred.resolve(token); }); //return promise return tokenDeferred.promise; } var getUserId = function() { var userIdDeferred = $q.defer(); userIdDeferred.resolve('someid'); return userIdDeferred.promise; } Now I have a list of topics that I would like to update as soon as these two promises get resolved var topics = { firstTopic: 'myApp.firstTopic

What are the ways to display 'chunked' responses as soon as they come into AngularJS?

我只是一个虾纸丫 提交于 2019-12-01 00:05:20
问题 Currently I have a problem displaying 'chunks' of responses that I am sending from my Web Service Node.js server (localhost:3000) to a simulated client running on a Node.js server (localhost:3001). edit * - Current implementation just uses Angular's %http as the transport without web-sockets The logic goes as follows: 1 . Create an array on the client side of 'Cities' and POST them (from the AngularJS controller) to the Web Service located at: localhost:3000/getMatrix $http({ method: 'POST',