q

How to add Promise to event handler in javascript

时光毁灭记忆、已成空白 提交于 2019-12-12 03:07:49
问题 Now I want to wrap amqp with Promise Q, here are the codes Sender.prototype.createConnection_ = function () { var deferred = Q.defer(); this.con_ = amqp.createConnection( this.connectOpt_, this.implementOpt_ ); deferred.resolve( this.con_ ); return deferred.promise; } Sender.prototype.connectionReady_ = function() { var deferred = Q.defer(), self = this; self.con_.on('ready', function() { console.log('connection is ok now'); deferred.resolve(self.con_); }); return deferred.promise; } Sender

Q promises chaining to do things in correct order

做~自己de王妃 提交于 2019-12-12 02:43:58
问题 I have a asynchronous function that needs to be called multiple times in the correct order. It's about uploading images to a server but like I said the images should be uploaded in the correct order. My function looks like this: function uploadImage(sku,fileName) { console.log("Uploading image for sku="+sku+", imageName="+fileName); var deferred = Q.defer(); var readStream = fs.createReadStream(rootPath+'/data/'+sku+"/"+fileName); var req = request.post('http://localhost:3000/'+sku+'/upload/'

Promises stop working when adding a insertion query

坚强是说给别人听的谎言 提交于 2019-12-12 02:18:32
问题 Asking about promises with Q library for node, mido helped me with a problem with the execution of promises in this thread. Basically, the solution was (almost) this code. var form= [ {'name':'FORM_NAME_1.1', 'label2':'FORM_LABEL_1.2' }, {'name':'FORM_NAME_2.1', 'label2':'FORM_LABEL_2.2' } ]; var params = ['params1','params2']; var meta = ['meta1','meta2']; let process = (currentValue,index,arr) => { //let reqCopy = {id: Math.random()} let reqCopy = {}; for(let attr in req) // copy all the

Why is no value returned from my Promise?

时光毁灭记忆、已成空白 提交于 2019-12-12 02:18:12
问题 In this slightly reduced code, one or more weeks of observations are downloaded a week at a time from an API, and aggregated into rows and exported to CSV. At least that is the idea. What is actually happening is that an Uncaught (in promise) TypeError: Cannot read property 'toString' of undefined is raised in the (not shown) exportToCsv function because the _promise pushed from promiseArray to rows is coming out as undefined . What am I missing? $("#downloadBtn").click(function() { weeks =

“Iterating” throw promises does not let to generate different ids

三世轮回 提交于 2019-12-12 02:17:53
问题 Reading some amazing tutorials about promises, I've discovered that, if I need to interate throw some promises, I can't use forEach or some other "traditional" iteration mechanisms, I have to use Q library for node, I've to "iterate" using Q.all . I've written a simple example in Nodejs Express in order to be sure I've understood promises: var form = [ {'name':'FORM_NAME_1.1', 'label2':'FORM_LABEL_1.2' }, {'name':'FORM_NAME_2.1', 'label2':'FORM_LABEL_2.2' } ]; var params = ['params1','params2

Viewing objects in javascript ( under the hood )

二次信任 提交于 2019-12-12 01:38:42
问题 Im very curious as to how objects are displayed in nodejs and in this case promises. When using console.log(promiseObject) the output is of type {state:pending} This seems very weird to me since a function .then() is invoked on that object so i would expect to see that there. Try for yourself with this code function a(){ var deferred = q.defer(); setTimeout(function(){ deferred.resolve(); },4000) return deferred.promise; } var p1 = a() console.log(p1) //outputs {state:pending} while i was

A synchronous Breeze ExecuteQuery

こ雲淡風輕ζ 提交于 2019-12-12 01:08:23
问题 I get data form the DB like this using Breeze promise ExecuteQuery : var getdata = function(){ var manager = new breeze.EntityManager(serviceName); var query = new EntityQuery().from('MyTable'); manager.executeQuery(query) .then(function(data){ //line1 console.log('success'); }); //line2 console.log('end'); } Is there any way to make this function synchronous : not executing line2 untill line1 is done (or query failed) ? Thanks 回答1: No. Once a function is async there is no real way to make

Resolve promise based on another promise

旧街凉风 提交于 2019-12-11 19:23:13
问题 I've a function, which returns a promise, inside that I call another function, and the status of this promise is based on the status of the inner promise. Is there a way to shorthand this process. Look at the example below. function foo(bar) { var deferred = Q.defer(); switch (bar) { case 'baz1': deferred.resolve(); break; case 'baz2': deferred.reject(); break; case 'this_is_how_i_do_it': funReturningPromise().then(function (value) { deferred.resolve(value); }, function (err) { deferred

double - triple for loops using index of vectors that vary in length

不羁的心 提交于 2019-12-11 15:49:58
问题 I spent too much time on this searching for documentation or adequate example to no avail. Kindly someone enlighten me how to deal with this problem. Say I have the following table of orders for buying a stock. They will end at the designated time. orders:([] seq:10*1+til 5; ID:5#`softbank;start:11:00 10:00 09:00 13:30 18:00;end:13:30 12:30 11:30 14:30 19:00) For some reason I am hoping to find the maximum number of orders alive (say none are transacted) at a sub-time interval within the

AngularJS $q.all update/notify not called why?

断了今生、忘了曾经 提交于 2019-12-11 12:09:42
问题 So I have a simple example of using $q.all to batch $resource calls, what I want to know is why is my update handler never called? I would have thought it would be called after each promise is successfully completed? Only the result handler is called. What am I doing wrong? Here is the code snippet: var promises = []; angular.forEach($scope.mappedData, function(item) { var resource = new Resource(item); promises.push(resource.$save()); }); $q.all(promises).then( function(result) { console.log