How to chain a variable number of promises in Q, in order?

There’s a nice clean way to to this with [].reduce. var chain = itemsToProcess.reduce(function (previous, item) { return previous.then(function (previousValue) { // do what you want with previous value // return your async operation return Q.delay(100); }) }, Q.resolve(/* set the first “previousValue” here */)); chain.then(function (lastResult) { // … }); reduce iterates through the … Read more

Promises: Repeat operation until it succeeds?

All the answers here are really complicated in my opinion. Kos has the right idea but you can shorten the code by writing more idiomatic promise code: function retry(operation, delay) { return operation().catch(function(reason) { return Q.delay(delay).then(retry.bind(null, operation, delay * 2)); }); } And with comments: function retry(operation, delay) { return operation(). // run the operation … Read more

What happens if i reject / resolve multiple times in Kriskowal’s q?

Since promises can only resolve once (to either fulfilled or rejected), the first resolution wins and any further calls will be ignored. From the docs: In all cases where a promise is resolved (i.e. either fulfilled or rejected), the resolution is permanent and cannot be reset. Attempting to call resolve, reject, or notify if promise … Read more

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

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 work just fine for you. But, there are more tools in the tool bin than just the basics and there are situations where … Read more

Rendering React components with promises inside the render method

render() method should render UI from this.props and this.state, so to asynchronously load data, you can use this.state to store imageId: imageUrl mapping. Then in your componentDidMount() method, you can populate imageUrl from imageId. Then the render() method should be pure and simple by rendering the this.state object Note that the this.state.imageUrls is populated asynchronously, … Read more

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

In simple terms, inside a then handler function: A) When x is a value (number, string, etc): return x is equivalent to return Promise.resolve(x) throw x is equivalent to return Promise.reject(x) B) When x is a Promise that is already settled (not pending anymore): return x is equivalent to return Promise.resolve(x), if the Promise was … Read more

How to check if an object is a Promise?

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 then method”. Promises will and should assimilate anything with a then method. All of the promise implementation you’ve mentioned do this. … Read more