Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. I want to use (native) promises in my frontend app to perform XHR request but without all the tomfoolery of a massive framework. I want my xhr to return
Tag: promise
How do you properly return multiple values from a Promise?
I’ve recently run into a certain situation a couple of times, which I didn’t know how to solve properly. Assume the following code: Now a situation might arise where I would want to have access to amazingData in afterSomethingElse. One obvious solution would be to return an array or a hash from afterSomething, because, well, you can only return one
How to check if an object is a Promise?
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? Answer 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 thenable which is basically “an object with a
How to fetch value from Promise object after promise has been resolved
Please note This is a contrived example. Problem I am unable to figure out how to return any kind of object/function for further downstream processing after shortAfterLongFunc completes. I can console.log from shortAfterLongFunc but that’s not what i require here. Fiddle Here Thanks for looking! UPDATE: Okay just to make my question slightly better…this is a simple use case I
Difference between microtask and macrotask within an event loop context
I’ve just finished reading the Promises/A+ specification and stumbled upon the terms microtask and macrotask: see http://promisesaplus.com/#notes I’ve never heard of these terms before, and now I’m curious what the difference could be? I’ve already tried to find some information on the web, but all I’ve found is this post from the w3.org Archives (which does not explain the difference
How are Promises implemented in Javascript without threads
Recently, I have been seeing the concept of Promises being implemented in AngularJS and JQuery. I have seen the implementation of a Futures in Java as in code below, however this requires concept of thread pools to be present in language/platform. However, there is no such threading concept in Javascript. How are Promises in Javascript implemented then ? Answer Promises
TypeError: Cannot read property ‘then’ of undefined
Above function return a string like “failed”. However, when I try to run then function on it, it will return error of and the cursor is indicate right after connected and before .then. Below is the full function: UPDATE Here is the islogged() function I am certain that the $checkSessionServer will result in a “failed” string. Nothing more. Answer You
How to return a resolved promise from an AngularJS Service using $q?
My service is: I’m calling this in my config file via: However, it complains that then is not a function. Aren’t I returning the resolved promise? Answer From your service method: And in your controller:
How to make a promise from setTimeout
This is not a realworld problem, I’m just trying to understand how promises are created. I need to understand how to make a promise for a function that returns nothing, like setTimeout. Suppose I have: How do I create a promise that async can return after the setTimeout is ready to callback()? I supposed wrapping it would take me somewhere:
Aren’t promises just callbacks?
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: Which I could use a library like async for anyway, with something like: Which is more code and less readable. I didn’t gain anything here, it’s not suddenly magically ‘flat’ either. Not to mention