q

angularjs simple .then or $q service in async requests

流过昼夜 提交于 2019-12-02 04:28:54
I don't know what is the exactly difference between AngularJS $q service and simply using .then() after async request. Simple example with .then() : function InboxService($http) { this.getEmails = function getEmails() { return $http.get('/emails'); }; } And when using the service (just part of code): InboxService.getEmails() .then(function (response) { // use response }); What is the difference with $q service with resolve and reject ? What is the difference with $q service with resolve and reject ? I assume you are asking about the usage of var deferred = $q.defer() with subsequent deferred

Refactoring: returning a promise from a value or an existing promise

空扰寡人 提交于 2019-12-02 03:59:13
问题 My scenario I used to have some node.js implementation done using callbacks but I am now refactoring my code to use Promises instead - using Q module. I have the following update() function where the inner _update() function already returns a Promise : exports.update = function(id, template, callback) { if (!_isValid(template)){ return callback(new Error('Invalid data', Error.INVALID_DATA)); } _update(id, template) // this already returns a promise .then(function() { console.log('UPDATE was

promise with loop and file read in nodejs

浪子不回头ぞ 提交于 2019-12-02 02:01:23
问题 I looked at lot of example but couldn't achieve it..so need help.. Problem.. the content from loop should be passed to execute one by one. each loop iteration contains a file read and database save operation along with few other object properties that need to be assigned. I have created example here.. http://runnable.com/VI1efZDJvlQ75mlW/api-promise-loop-for-node-js-and-hello-world how to run: Api: http://web-91b5a8f5-67af-4ffd-9a32-54a50b10fce3.runnable.com/api/upload method : POST content

Difference between “Q” and “q” in angularjs and requirejs

不打扰是莪最后的温柔 提交于 2019-12-02 01:59:57
I am creating a single page app built on AngularJS, Breeze, and RequireJS. In setting up AMD with requirejs to work with Angular and Breeze, I encountered an issue with Breeze's dependency on "q". If the configuration rule for "q" is lowercase, even if there is no explicit export in the "shim", Breeze gives this error: Uncaught Error: Unable to initialize Q. See https://github.com/kriskowal/q "http://localhost:1498/Scripts/shared/breeze.js"breeze.js:1` When require config changes all references from "q" to "Q" (even without the export), the code works. Does anyone know why this is happening?

promise with loop and file read in nodejs

旧巷老猫 提交于 2019-12-02 00:46:24
I looked at lot of example but couldn't achieve it..so need help.. Problem.. the content from loop should be passed to execute one by one. each loop iteration contains a file read and database save operation along with few other object properties that need to be assigned. I have created example here.. http://runnable.com/VI1efZDJvlQ75mlW/api-promise-loop-for-node-js-and-hello-world how to run: Api: http://web-91b5a8f5-67af-4ffd-9a32-54a50b10fce3.runnable.com/api/upload method : POST content-type : multipart/form-data upload more than one file with name. .. the final expected promise is files

Linting Promises in Javascript

試著忘記壹切 提交于 2019-12-01 17:54:20
问题 I am looking to standardize the use of Q promises in my team's codebase. Are there any good jscs extensions (or other linters) to help enforce style when it comes to promises? We would like our promises to follow this form: promise() .then() .catch() .done(); And would like a linter to catch any .then() in our code that is missing a .catch() Advice for other stylistic tips when it comes to promises is welcome too. 回答1: @Jeff that approach looks as total overkill. Neither of this functions

$q.all and nested promises

六眼飞鱼酱① 提交于 2019-12-01 17:49:01
Have a question about synchronizing nested promises when using $q in Angular. Will the following code ensure that the entire chain of promises is waited for? Meaning will the nested calls to services that return promises be waited for in the $q.all block? var call1 = service1.get('/someUr').then(function(){ return service2.get('/someUrl2'); //returns promise }); var call2 = service3.get('/someUr').then(function(){ return 'hello'; }); var call3 = service4.get('/someUr').then(function(){ return service3.get('/someUrl3');//returns promise }); $q.all(call1,call2,call3).then(function(){ console.log

Returning an Array using Firebase

假如想象 提交于 2019-12-01 13:36:39
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.promise; } function getPost( post ) { var deferred = q.defer(); deferred.resolve(post.val()); return deferred

throw Error after promise is rejected - Q

℡╲_俬逩灬. 提交于 2019-12-01 11:33:12
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 exception, however, it is not thrown / ignored. throw new Error('I want to throw this exception'); } );

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

血红的双手。 提交于 2019-12-01 09:01:26
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(function (result) { //process data processedResult = 'some stuff'; deferred.resolve(processedResult); }) .fail