q

What's the difference between returning value or Promise.resolve from then()

 ̄綄美尐妖づ 提交于 2019-11-26 18:02:53
What is the difference between: new Promise(function(res, rej) { res("aaa"); }) .then(function(result) { return "bbb"; }) .then(function(result) { console.log(result); }); and this: new Promise(function(res, rej) { res("aaa"); }) .then(function(result) { return Promise.resolve("bbb"); }) .then(function(result) { console.log(result); }); I'm asking as I'm getting different behaviour Using Angular and $http service with chaining .then(). A bit too much code hence first the example above. Hrishi The rule is, if the function that is in the then handler returns a value, the promise resolves/rejects

Why is 'this' undefined inside class method when using promises? [duplicate]

女生的网名这么多〃 提交于 2019-11-26 17:27:25
This question already has an answer here: setTimeout and “this” in JavaScript 5 answers I have a javascript class, and each method returns a Q promise. I want to know why this is undefined in method2 and method3 . Is there a more correct way to write this code? function MyClass(opts){ this.options = opts; return this.method1() .then(this.method2) .then(this.method3); } MyClass.prototype.method1 = function(){ // ...q stuff... console.log(this.options); // logs "opts" object return deferred.promise; }; MyClass.prototype.method2 = function(method1resolve){ // ...q stuff... console.log(this); //

return value after a promise [duplicate]

非 Y 不嫁゛ 提交于 2019-11-26 15:22:31
This question already has an answer here: setting a variable to get return from call back function using promise 2 answers How to return value from an asynchronous callback function? [duplicate] 3 answers I have a javascript function where I want to return the value that I get after the return method. Easier to see than explain function getValue(file){ var val; lookupValue(file).then(function(res){ val = res.val; } return val; } What is the best way to do this with a promise. As I understand it, the return val will return before the lookupValue has done it's then, but the I can't return res

Replacing callbacks with promises in Node.js

霸气de小男生 提交于 2019-11-26 15:02:34
问题 I have a simple node module which connects to a database and has several functions to receive data, for example this function: dbConnection.js: import mysql from 'mysql'; const connection = mysql.createConnection({ host: 'localhost', user: 'user', password: 'password', database: 'db' }); export default { getUsers(callback) { connection.connect(() => { connection.query('SELECT * FROM Users', (err, result) => { if (!err){ callback(result); } }); }); } }; The module would be called this way from

How to chain a variable number of promises in Q, in order?

↘锁芯ラ 提交于 2019-11-26 12:09:08
问题 I have seen Chaining an arbitrary number of promises in Q ; my question is different. How can I make a variable number of calls, each of which returns asynchronously, in order? The scenario is a set of HTTP requests, the number and type of which is determined by the results of the first HTTP request. I\'d like to do this simply. I have also seen this answer which suggests something like this: var q = require(\'q\'), itemsToProcess = [\"one\", \"two\", \"three\", \"four\", \"five\"]; function

While loop with promises

守給你的承諾、 提交于 2019-11-26 12:06:12
What would be the idiomatic way to do something like a while loop with promises. So: do something if the condition still stands do it again repeat then do something else. dosomething.then(possilblydomoresomethings).then(finish) I've done it this way I was wondering if there were any better/more idomatic ways? var q = require('q'); var index = 1; var useless = function(){ var currentIndex = index; console.log(currentIndex) var deferred = q.defer(); setTimeout(function(){ if(currentIndex > 10) deferred.resolve(false); else deferred.resolve(true); },500); return deferred.promise; } var control =

Angularjs $q.all

自闭症网瘾萝莉.ら 提交于 2019-11-26 12:00:43
I have implemented the $q.all in angularjs, but I can not make the code work. Here is my code : UploadService.uploadQuestion = function(questions){ var promises = []; for(var i = 0 ; i < questions.length ; i++){ var deffered = $q.defer(); var question = questions[i]; $http({ url : 'upload/question', method: 'POST', data : question }). success(function(data){ deffered.resolve(data); }). error(function(error){ deffered.reject(); }); promises.push(deffered.promise); } return $q.all(promises); } And here is my controller which call the services: uploadService.uploadQuestion(questions).then

Produce a promise which depends on recursive promises

随声附和 提交于 2019-11-26 11:36:33
问题 I have an array of integer ids, such as var a=[1,2,3,4,5] and I have a need to perform asynchronous remote calls for each of these ids. Each call is a WebAPI request performed using $resource and presented as promise. I need to create a function that takes array of these IDs, then initializes recursive promises chain. The chain should result in consequential webapi calls for each of IDs, one by one. These calls should not be parallel but chained. The function in question returns itself a \

What happens if i reject / resolve multiple times in Kriskowal&#39;s q?

情到浓时终转凉″ 提交于 2019-11-26 11:28:34
问题 I\'m studying the promises pattern and using kriskowal\'s q for node.js, having this snippet: var deferred = Q.defer(); try { messageData = JSON.parse(message); } catch (e) { global.logger.warn(\'Error parsing JSON message.\'); deferred.reject(e); } ... if (some_reason) deferred.resolve(something); ... return deferred.promise; What if both the parser fails and some_reason is true? Will the execution procede from rejecting through resolving and both promise\'s method be called at different

How do I sequentially chain promises with angularjs $q?

北战南征 提交于 2019-11-26 11:14:14
问题 In the promise library Q , you can do the following to sequentially chain promises: var items = [\'one\', \'two\', \'three\']; var chain = Q(); items.forEach(function (el) { chain = chain.then(foo(el)); }); return chain; however, the following doesn\'t work with $q : var items = [\'one\', \'two\', \'three\']; var chain = $q(); items.forEach(function (el) { chain = chain.then(foo(el)); }); return chain; 回答1: Redgeoff, your own answer is the way I used to translate an array into a chained