Understanding promise.race() usage

As you see, the race() will return the promise instance which is firstly resolved or rejected: var p1 = new Promise(function(resolve, reject) { setTimeout(resolve, 500, ‘one’); }); var p2 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, ‘two’); }); Promise.race([p1, p2]).then(function(value) { console.log(value); // “two” // Both resolve, but p2 is faster }); For a scenes … Read more

Why do both Promise’s then & catch callbacks get called?

The then callback gets called because the catch callback is before it, not after. The rejection has already been handled by catch. If you change the the order (i.e. (promise.then(…).catch(…))), the then callback won’t be executed. MDN says that the .catch() method “returns a new promise resolving to the return value of the callback”. Your … Read more

Awaited but never resolved/rejected promise memory usage [duplicate]

Preface (you probably know this!): await is syntactic sugar for using promise callbacks. (Really, really, really good sugar.) An async function is a function where the JavaScript engine builds the promise chains and such for you. Answer: The relevant thing isn’t so much whether the promise is settled, but whether the promise callbacks (and the … Read more

How do you implement a “raceToSuccess” helper, given a list of promises?

This is a classic example where inverting your logic makes it much clearer. Your “race” in this case is that you want your rejection behavior to in fact be success behavior. function oneSuccess(promises){ return Promise.all(promises.map(p => { // If a request fails, count that as a resolution so it will keep // waiting for other … 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

Can I fire and forget a promise in nodejs (ES7)?

Yes, you can do that, and it will run the two asynchronous functions in parallel. You’ve just created a promise and thrown it away. However, this means that when the promise is rejected you won’t notice. You’ll just get an unhandledRejection eventually which will crash your process if not handled. Is this OK? How can … Read more

What is the difference between “event loop queue” and “job queue”?

Why “1” is after “b”? The promise specification states that all promise .then() handlers must be called asynchronously after the call stack has emptied. Thus, both a and b, which are executed synchronously on the call stack will execute before any .then() handlers so 1 will always be after a and b. Some interesting reading: … Read more

techhipbettruvabetnorabahisbahis forumueduseduseduseduedusedueduseduedusedu