q

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

浪尽此生 提交于 2019-11-26 08:54:12
问题 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

Rendering React components with promises inside the render method

戏子无情 提交于 2019-11-26 08:04:42
问题 I have a component which gets a collection of items as props and map s them to a collection of components which are rendered as children of a parent component. We use images stored in WebSQL as byte arrays. Within the map function I get an image Id from the item and make an async call to the DAL in order to get the byte array for the image. My problem is that I cannot propagate the promise in to React, since it was not designed to deal with promises in rendering (not as far as I can tell

How do I tell if an object is a Promise?

限于喜欢 提交于 2019-11-26 06:14:48
问题 Whether it\'s an ES6 Promise or a bluebird Promise, Q Promise, etc. How do I test to see if a given object is a Promise? 回答1: How a promise library decides If it has a .then function - that's the only standard promise libraries use. The Promises/A+ specification has a notion called then able which is basically "an object with a then method". Promises will and should assimilate anything with a then method. All of the promise implementation you've mentioned do this. If we look at the

Are there still reasons to use promise libraries like Q or BlueBird now that we have ES6 promises? [closed]

守給你的承諾、 提交于 2019-11-26 06:11:00
问题 After Node.js added native support for promises, are there still reasons to use libraries like Q or BlueBird? For example if you are starting a new project and let\'s assume in this project you don\'t have any dependencies that use these libraries, can we say that there are really no more reasons to use such libraries? 回答1: The old adage goes that you should pick the right tool for the job. ES6 promises provide the basics. If all you ever want or need is the basics, then that should/could

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

孤街浪徒 提交于 2019-11-26 04:40:31
问题 This question already has answers here : setTimeout and “this” in JavaScript (5 answers) Closed 3 years ago . 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\"

return value after a promise [duplicate]

风流意气都作罢 提交于 2019-11-26 03:39:02
问题 This question already has answers 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) Closed 5 years ago . 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

Angularjs $q.all

[亡魂溺海] 提交于 2019-11-26 02:40:07
问题 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)

How to sequentially run promises with Q in Javascript?

左心房为你撑大大i 提交于 2019-11-26 02:19:44
I am having a hard time running promises sequentially. var getDelayedString = function(string) { var deferred = Q.defer(); setTimeout(function() { document.write(string+" "); deferred.resolve(); }, 500); return deferred.promise; }; var onceUponATime = function() { var strings = ["Once", "upon", "a", "time"]; var promiseFuncs = []; strings.forEach(function(str) { promiseFuncs.push(getDelayedString(str)); }); //return promiseFuncs.reduce(Q.when, Q()); return promiseFuncs.reduce(function (soFar, f) { return soFar.then(f); }, Q()); }; getDelayedString("Hello") .then(function() { return

Aren&#39;t promises just callbacks?

隐身守侯 提交于 2019-11-26 01:18:20
问题 I\'ve been developing JavaScript for a few years and I don\'t understand the fuss about promises at all. It seems like all I do is change: api(function(result){ api2(function(result2){ api3(function(result3){ // do work }); }); }); Which I could use a library like async for anyway, with something like: api().then(function(result){ api2().then(function(result2){ api3().then(function(result3){ // do work }); }); }); Which is more code and less readable. I didn\'t gain anything here, it\'s not

How to sequentially run promises with Q in Javascript?

≡放荡痞女 提交于 2019-11-26 01:09:10
问题 I am having a hard time running promises sequentially. var getDelayedString = function(string) { var deferred = Q.defer(); setTimeout(function() { document.write(string+\" \"); deferred.resolve(); }, 500); return deferred.promise; }; var onceUponATime = function() { var strings = [\"Once\", \"upon\", \"a\", \"time\"]; var promiseFuncs = []; strings.forEach(function(str) { promiseFuncs.push(getDelayedString(str)); }); //return promiseFuncs.reduce(Q.when, Q()); return promiseFuncs.reduce