How do you properly promisify request?

The following should work: var request = Promise.promisify(require(“request”)); Promise.promisifyAll(request); Note that this means that request is not a free function since promisification works with prototype methods since the this isn’t known in advance. It will only work in newer versions of bluebird. Repeat it when you need to when forking the request object for cookies. … Read more

Is providing a Promise as a module’s export a valid pattern for asynch initialization in Node.js?

This seems like a perfectly good interface for a module who’s job is to do a one-time fetch of some data. The data is obtained async so a promise makes sense for that. The goal is to fetch the data just once and then let all places this module gets used just have access to … Read more

How to actually use Q promise in node.js?

promiseMeSomething() is going to return a Q promise object, which will have then function in it, which is defined, like this Promise.prototype.then = function (fulfilled, rejected, progressed) { The simplest way to create a Promise object would be to use the Q function constructor, like this new Q(value) will create a new promise object. You … Read more

How can I Interleave / merge async iterables?

There is no way to write this with a loop statement. async/await code always executes sequentially, to do things concurrently you need to use promise combinators directly. For plain promises, there’s Promise.all, for async iterators there is nothing (yet) so we need to write it on our own: async function* combine(iterable) { const asyncIterators = … Read more

Waiting for a promise?

This is the most basic situation that promises are for. You simply need to make a promise with var deferred = $q.defer() when beginning an async operation, resolve the promise with deferred.resolve(result) when the async operation is complete, and return deferred.promise in your function. Angular’s asynchronous methods do this internally and return promises already, so … Read more

Get a value from a Promise Typescript

How do I unwrap/yield the value inside that promise You can do it with async/await.Don’t be fooled into thinking that you just went from async to sync, async await it is just a wrapper around .then. functionA(): Promise<string> { // api call returns Promise<string> } async functionB(): Promise<string> { const value = await this.functionA() // … 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

What is the relationship between event loop and Promise [duplicate]

Each event loop has a microtask queue and a macrotask queue. A microtask is a task that is originally to be queued on the microtask queue rather than a task queue. Refer to https://www.w3.org/TR/html51/webappapis.html#microtask-queue. There are two kinds of microtasks: solitary callback microtasks, such as Promise, and compound microtasks, such as Object.observe, MutationObserver and process.nextTick … Read more

Execute batch of promises in series. Once Promise.all is done go to the next batch

An answer from October 2020. Async/await makes it short: only 10 code lines+JSDoc. /** * Same as Promise.all(items.map(item => task(item))), but it waits for * the first {batchSize} promises to finish before starting the next batch. * * @template A * @template B * @param {function(A): B} task The task to run for each item. … Read more

How to catch error in Observable.forkJoin(…)?

You may catch the error in each of your observables that are being passed to forkJoin: // Imports that support chaining of operators in older versions of RxJS import {Observable} from ‘rxjs/Observable’; import {forkJoin} from ‘rxjs/add/observable/forkJoin’; import {of} from ‘rxjs/add/observable/of’; import {map} from ‘rxjs/add/operator/map’; import {catch} from ‘rxjs/add/operator/catch’; // Code with chaining operators in older … Read more