Skip to content

Tag: promise

How do I promisify native XHR?

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 mass…

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 af…

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…

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…

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 suppose…

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 s…