q

When to reject/resolve a promise

蹲街弑〆低调 提交于 2019-11-30 22:12:47
问题 I am thinking about when exactly I need to reject a promise. I found a couple of questions regarding this topic, but could not find a proper answer. When should I reject a promise? This article http://howtonode.org/6666a4b74d7434144cff717c828be2c3953d46e7/promises says: Resolve: A successful Promise is 'resolved' which invokes the success listeners that are waiting and remembers the value that was resolved for future success listeners that are attached. Resolution correlates to a returned

How to populate Angular UI Bootstrap Typeahead with newest $resource

让人想犯罪 __ 提交于 2019-11-30 20:23:10
According to this Paweł Kozłowski's answer, Typeahead from AngularUI-Bootstrap should work when asynchronously obtaining popup items with $resource in newest Angular versions (I'm using 1.2.X). Plunk - Paweł's version - Typeahead with $http I guess I don't know how to use it properly (As a result I get an error in typeaheadHighlight directive's code - typeahead treats instantly returned Resource s as strings and tires to highlight them). Plunk - Typeahead with $resource I think the critical code is: $scope.cities = function(prefix) { var p = dataProviderService.lookup({q: prefix}).$promise;

Promises and generic .catch() statements

蓝咒 提交于 2019-11-30 18:30:45
I'm writing an API for my system, that's sending an XHR to the server and returns a promise that should be handled by the caller - so far so good. For each API call I must use a .then and .catch calls, but usually (like 75% of the time) the .catch references the same functionality which simply prints using console.error . My question is - Is there a way to attach a default catch statement for each promise that I create? (that let's say prints to the console), and for each promise that I would like to further handle the rejection, I would add another .catch call (or even override it)?

How can I promise-ify a one-off usage of gulp in my application?

这一生的挚爱 提交于 2019-11-30 18:19:01
As part of a small program I'm writing, I would like to use gulp to convert a large set of a files to markdown. This is not part of a build step separate from the program. It's a part of the program. So I'm not using a gulpfile to handle this. The problem is, since it's async, I want to use a promise which will alert me when the gulp task is finished. Something like this would be ideal: io.convertSrc = function() { var def = q.defer(); gulp.src(src + '/*.md') .pipe(marked({})) .pipe(gulp.dest(dist), function() { def.resolve('We are done!'); }); return def.promise; } But pipe doesn't take a

How to configure additional classpath in SpringBoot?

依然范特西╮ 提交于 2019-11-30 17:51:44
I want to make standalone web application. I have some problem with SpringBoot. My application is one jar file from SpringBoot. But my application was usually needed jdbc driver jar. I want to exclude jdbc driver jar for my application. I want to read library jar from lib folder. But SpringBoot lib folder is BOOT-INF/lib is final static . So, I want to add external classpath (lib) for jdbc driver jar. How to configure additional classpath in SpringBoot. Is it available? You may refer this below link from spring boot: https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar

Q promise: are callbacks invoked in the same order as registered?

别说谁变了你拦得住时间么 提交于 2019-11-30 17:48:39
I'm using the Q promise library. My code relies on the fact that the callbacks for a single promise are executed in the same order as they were registered. http://jsfiddle.net/HgYtK/1/ var deferred = Q.defer(); var promise = deferred.promise; ['first', 'second', 'third'].forEach(function (position) { promise.then(function () { alert(position); }); }); deferred.resolve(); This does produce the correct result, but I don't know if it's part of the spec or a happy coincidence that could break down the line. Ilia Choly From the Promises/A+ Spec 2.2.6.1 If/when promise is fulfilled, respective

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

荒凉一梦 提交于 2019-11-30 15:37:20
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? First of all - JavaScript as a language does not say anything about concurrency. In fact, in a lot of scenarios

Skipping promise chain after handling error

强颜欢笑 提交于 2019-11-30 15:07:38
问题 Using the https://github.com/kriskowal/q library, I'm wondering if it's possible to do something like this: // Module A function moduleA_exportedFunction() { return promiseReturningService().then(function(serviceResults) { if (serviceResults.areGood) { // We can continue with the rest of the promise chain } else { performVerySpecificErrorHandling(); // We want to skip the rest of the promise chain } }); } // Module B moduleA_exportedFunction() .then(moduleB_function) .then(moduleB

Skipping promise chain after handling error

可紊 提交于 2019-11-30 13:23:30
Using the https://github.com/kriskowal/q library, I'm wondering if it's possible to do something like this: // Module A function moduleA_exportedFunction() { return promiseReturningService().then(function(serviceResults) { if (serviceResults.areGood) { // We can continue with the rest of the promise chain } else { performVerySpecificErrorHandling(); // We want to skip the rest of the promise chain } }); } // Module B moduleA_exportedFunction() .then(moduleB_function) .then(moduleB_anotherFunction) .fail(function(reason) { // Handle the reason in a general way which is ok for module B functions

Stop memory leaks with recursive promises

て烟熏妆下的殇ゞ 提交于 2019-11-30 11:42:20
How do I create a recursive chain of JavaScript Promise s with the Q library ? The following code fails to complete in Chrome: <html> <script src="q.js" type="text/javascript"></script> <script type="text/javascript"> //Don't keep track of a promises stack for debugging //Reduces memory usage when recursing promises Q.longStackJumpLimit = 0; function do_stuff(count) { if (count==1000000) { return; } if (count%10000 == 0){ console.log( count ); } return Q.delay(1).then(function() { return do_stuff(count+1); }); } do_stuff(0) .then(function() { console.log("Done"); }); </script> </html> This won