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

While loop with promises

Here’s a reusable function that I think is pretty clear. var Q = require(“q”); // `condition` is a function that returns a boolean // `body` is a function that returns a promise // returns a promise for the completion of the loop function promiseWhile(condition, body) { var done = Q.defer(); function loop() { // When … Read more

Angularjs $q.all

In javascript there are no block-level scopes only function-level scopes: Read this article about javaScript Scoping and Hoisting. See how I debugged your code: var deferred = $q.defer(); deferred.count = i; console.log(deferred.count); // 0,1,2,3,4,5 –< all deferred objects // some code .success(function(data){ console.log(deferred.count); // 5,5,5,5,5,5 –< only the last deferred object deferred.resolve(data); }) When you … 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