q

Using promises - Logging stack trace in fail handler

倾然丶 夕夏残阳落幕 提交于 2019-12-03 22:06:37
I am rather new to nodejs so I will explain in a bit more detail what I am trying to do. I have a webserver. If a request fails I want to log the stack trace of that exception, but deliver a error page and not crash the server. As an example, the function handling the requests: var Q = require('q'); var requestHandler = function () { // Here I get the data etc. that was requested. As this is not important, just a dummy here Q.resolve() // Now I answer the request .then(function (data) { // Dummy Code representing the sending of a response console.log('sending response …'); console.log('oh no!

Is promise.all useful given that javascript is executed in a single thread?

十年热恋 提交于 2019-12-03 20:12:42
问题 In, for example, kriskowal's Q, one can do something like: promise1.then(function(p1){ var p2 = makePromise2(); var p3 = makePromise3(); var p4 = makePromise4(); return [p2, p3, p4]; }) .all(promises, function(){ console.log('all promises fulfilled'); }, function(reason){ console.log('a promise was rejected: ' + reason.toString()); }); Given that javascript executes in a single thread, does this have any benefit, performance or otherwise, over simply doing a series of then() calls? 回答1: First

How to use “q” module for refactoring mongoose code?

怎甘沉沦 提交于 2019-12-03 19:20:49
问题 I'm using mongoose to insert some data into mongodb. The code looks like: var mongoose = require('mongoose'); mongoose.connect('mongo://localhost/test'); var conn = mongoose.connection; // insert users conn.collection('users').insert([{/*user1*/},{/*user2*/}], function(err, docs) { var user1 = docs[0], user2 = docs[1]; // insert channels conn.collection('channels').insert([{userId:user1._id},{userId:user2._id}], function(err, docs) { var channel1 = docs[0], channel2 = docs[1]; // insert

Returning a value from a Promise

早过忘川 提交于 2019-12-03 17:36:45
问题 I would like to call the Google Maps Geocoding API using a Promise like this: function makeGeoCodingRequest(address,bounds) { /* Input parameters: address:a string bounds: an object of class google.maps.LatLngBounds(southWest,northEast) This will return a set of locations from the google geocoding library for the given query */ var url="https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=AIzaSyD9GBloPC20X-1kWRo7sm_0z5xvCiaSd3c"; var promise,response; var messages={

$q 'finally' not working in IE8

可紊 提交于 2019-12-03 11:07:10
Angular 1.2 replaced always with finally on promises. So what was once this: $http.get('/myurl').always(handler); Now needs to be this: $http.get('/myurl').finally(handler); But I am getting the error "expected identifier" in IE8. How can I make this work in IE8? Found it: https://github.com/angular/angular.js/commit/f078762d48d0d5d9796dcdf2cb0241198677582c $http.get('/myurl')["finally"](handler); 来源: https://stackoverflow.com/questions/18254205/q-finally-not-working-in-ie8

q.js: difference between resolve() and fulfill()

早过忘川 提交于 2019-12-03 10:29:32
I'm still unclear on the difference between calling a resolver's resolve() vs fulfill()? I see both the functions and the terms "resolve a promise" and "fulfill a promise" batted around a lot. When should I be using each? You should use resolve . deferredPromise.resolve(nextPromise) means that anything waiting for deferredPromise will now wait for nextPromise . If nextPromise is not a promise at all, it gets turned into a fulfilled promise which goes on to inform anything waiting for it that the value has become available. The fulfill method is a bad idea that will be deprecated and eventually

Returning a value from a Promise

浪尽此生 提交于 2019-12-03 06:56:38
I would like to call the Google Maps Geocoding API using a Promise like this: function makeGeoCodingRequest(address,bounds) { /* Input parameters: address:a string bounds: an object of class google.maps.LatLngBounds(southWest,northEast) This will return a set of locations from the google geocoding library for the given query */ var url="https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=AIzaSyD9GBloPC20X-1kWRo7sm_0z5xvCiaSd3c"; var promise,response; var messages={ "ZERO_RESULTS":"No results were found", "OVER_QUERY_LIMIT":"We are over the query limit.Wait awhile

Multiple chained deferred functions using q in AngularJS stop returning data

前提是你 提交于 2019-12-03 02:18:16
问题 I am trying to chain together multiple deferred function calls such that the next call gets the results of the previous deferred.resolve. When I chain together more than 2 of these calls, the data stops being returned. Here is the basic code inside an angular controller: $scope.runAsync = function() { var asyncFn1 = function(data){ var deferred = $q.defer(); $timeout(function(){ console.log("Async fn1 " + data); $scope.outputLines.push("Async fn1 " + data); deferred.resolve("Async fn1 " +

How does Angular $q.when work?

自古美人都是妖i 提交于 2019-12-03 02:06:44
问题 Can some one explain me how does $q.when work in AngularJS? I'm trying to analyse how $http work and found this: var promise = $q.when(config); And here is config object from Chrome console: Object {transformRequest: Array[1], transformResponse: Array[1], cache: Object, method: "GET", url: "/schedule/month_index.html"…} cache: Object headers: Object method: "GET" transformRequest: Array[1] transformResponse: Array[1] url: "/schedule/month_index.html" __proto__: Object What happens next? How

Mongoose and promises: how to get an array of query results?

对着背影说爱祢 提交于 2019-12-02 21:00:50
Using mongoose to query results from the db and Q for promises, but finding it hard to wrap my head around just getting a list of users that's available. Currently I have some something like this: var checkForPerson = function( person ) { people = mongoose.model('Person', Person) return people.findOne({"_id": person }, function(err, doc) { if (err) console.log(err) if (doc !== null) { return doc } else { console.log('no results') } }) } var promises = someArrayOfIds.map(checkForPerson); // this is where I would like to have an array of models var users = Q.all(promises) //this fires off before