Search Posts

Category: es6-promise

Where are resolve and reject defined in Promises?

I’m currently following a tutorial on Angular Promises, but I don’t understand how the resolve and reject functions can be executed if they aren’t defined anywhere. I thought I the programmer had to define these functions? Here’s the tutorial. https://toddmotto.com/promises-angular-q Here’s the code. const handleThirdPartyCallback = someArgument => { let promise = new Promise((resolve, reject) => { // assuming some third-party API, that is *not* a Promise Object // but fires a callback once finished […]

What does AngularJS $resource.delete() return?

I am using AngularJS $resource to list, create, and update resources and it works fine. For example: getFoo = (fooId) => { const Foo = $resource(“api/foos/:fooId); return Foo.get({fooId}); }; This returns a Foo instance that has a Foo.$promise I can use to see when the request finishes. But what if I want to delete a Foo? deleteFoo = (fooId) => { const Foo = $resource(“api/foos/:fooId); return Foo.delete({fooId}); }; All the examples I can find show […]

Promise resolve methods unaccessible from outside function scope in $transitions onstart hook

I am trying to implement a scenario where user is warned before navigating away from a particular route(AngularJS UI-router) and based on the user choice(YES/NO) the route transition is proceeded or aborted. I am trying to use $transitions onStart lifecycle hook for tracking the route change. As per the UI-router documentation https://ui-router.github.io/guide/transitionhooks#promises , I am returning a promise which resolves from other controller methods(which are called when user selects a choice in the warning popup). […]

Setting a timeout for each promise within a promise.all

I am able to successfully perform a Promise.all, and gracefully handle resolves and rejects. However, some promises complete within a few milliseconds, some can/could take a while. I want to be able to set a timeout for each promise within the Promise.all, so it can attempt to take a maximum of say 5seconds. getData() { var that = this; var tableUrls = [‘http://table-one.com’,’http://table-two.com’]; var spoonUrls = [‘http://spoon-one.com’,’http://spoon-two.com’]; var tablePromises = that.createPromise(tableUrls); var spoonPromises = that.createPromise(spoonUrls); […]

Service returning before all http calls have completed

I have a controller calling a service, which performs a number of http calls and returns back a set of data to my controller. The problem i am facing is that my service returns some data (typically emtpy) before all the http calls have been completed, and as a result, $scope.myForksLinks is undefined. Controller: myService.getMyData() .then(function(response) { $scope.myUrl = response.myForksLinks; }); MyService: handleResolveReject() { return promise.then(function(v) { return {v:v, status: “resolved”}; }, function(e) { return […]

$q in Angular or Promises in ES6

I have a project using ES6 and AngularJS, and as far as I found, AngularJS $q promises is synchronous but ES6 promises not. Right? The question is, what are the differences? and when should I use $q or ES6? I know what is the difference between Synchronous and Asynchronous, but got confused between new Promise and AngularJS $q. raise from here Source: AngularJS

How to convert method created to return a promise with $q library to use an ES6 Promise. Angularjs app to Angular4+

Since the ES6 Promise does not have a deferred object confused on how to go about converting this to work with ES6 Promises. One solution that I was looking at is to add a deffered object manually to the Promise Constructor. see the No deffered section for the example code Reason: I am converting the angularjs app to angular4 and using it to better understand how to work with ES6 Promises. I was going to […]

ES6 Promise error not bubbling up as expected [duplicate]

This question already has an answer here: Why is the Promise reject() in my jest.mock going to a then() rather than a catch()? 2 answers Is there an easier/proper way for when a promise error’s for it to bubble up to when it is called in a second promise without having to reject the promise in it’s catch block each time manually? promise1() { return this.$http .get(“/url”) .then(response => { // do things }) .catch(err […]