Returning value instead of promise in useEffect in React

I have the following code, let currencyValues = []; const retrieveCurrencyPairs = async (pair) => { const r = await fetch(pair.url); const rParsed = await r.json(); let newCurrencyValue = { currency: pair.countries, value: rParsed }; return newCurrencyValue; }; useEffect(() => { currencyValues = currencyPairs.map(retrieveCurrencyPairs); console.log(currencyValues); }, []); and the following expectations: On page load, the… Read More Returning value instead of promise in useEffect in React

Calling .finally on a promise that is try/catch handled elsewhere causes UnhandledPromiseRejection in nodejs

I’ve recently encountered a weird behavior with nodejs. I’ll explain with the following example: let’s say we have two functions, foo and bar, foo creates a promise, calls .finally() on it, and returns it. bar on the other hand, wraps the promise with a try/catch. const foo = () => { const promise = new… Read More Calling .finally on a promise that is try/catch handled elsewhere causes UnhandledPromiseRejection in nodejs

Async function works with a callback does not work with await

I am trying to integrate into my code an async library function. It works fine as: client.checkPermission(checkPermissionRequest, (err, response) => { if (err || response?.permissionship !== HAS_PERMISSION) { const e = err || new Error(‘You do not have access to event:’, id); res.code(404).send({ error: e }); } }); I want to rewrite it in an… Read More Async function works with a callback does not work with await

Why Promise.race([Promise.resolve(1), 2]).then(console.log) returns 1 instead of 2?

Why does this behavior happen with Promise.race(), that Promise.resolve(1) calculates faster rather than just 2? Promise.race([Promise.resolve(1), 2]).then(console.log) >Solution : From MDN docs If the iterable contains one or more non-promise value and/or an already settled promise, then Promise.race will settle to the first of these values found in the array const foreverPendingPromise = Promise.race([]); const… Read More Why Promise.race([Promise.resolve(1), 2]).then(console.log) returns 1 instead of 2?

cannot call promise inside of function: "has no call signatures"

I have this class: export class ExponentialBackoffUtils { public static retry(promise: Promise<any>, maxRetries: number, onRetry?: Function) { function waitFor(milliseconds: number) { return new Promise((resolve) => setTimeout(resolve, milliseconds)); } async function retryWithBackoff(retries: number) { try { if (retries > 0) { const timeToWait = 2 ** retries * 1000; await waitFor(timeToWait); } console.log(‘Retries: ‘, retries); if… Read More cannot call promise inside of function: "has no call signatures"

Binding multiple callbacks for promises returns resolve value of first promise, not second

I have the following code as part of some coding practice I am doing: var promise = new Promise(function(resolve, reject) { setTimeout(function() { resolve(‘hello world’); }, 2000); }); var promiseTwo = new Promise((resolve, reject) => { setTimeout(() => { resolve(‘hello again!’); }, 2000); }); promise.then(promiseTwo).then((data) => { console.log(data) }) When the data is logged, it… Read More Binding multiple callbacks for promises returns resolve value of first promise, not second

how to assign a top-level const from a promise when top-level await is not available

I’m trying to assign a const at the top-level, where the value needs to come from an asynchronous function. However the version of nodejs on the production server I am using is not new enough to support await, so I can’t do something like this: const value = await f(x) I can’t declare the variable… Read More how to assign a top-level const from a promise when top-level await is not available

javascript – working with multiple promises inside loop – how to return data outside of promise?

I’m struggling to understand how I can return data from multiple promises to build up an array of data. Is there anyway I can return the data outside of the promise to push to the data variable? I have the following: db_sh.find({ selector: {sh: req.params.sh_id}, fields: [‘_id’, ‘sh_id’, ‘time’], sort: [‘_id’] }).then(function (result) { let… Read More javascript – working with multiple promises inside loop – how to return data outside of promise?

How does promise.all handle multiple API calls when one of them fails?

I had a question on how promise.all handles rejection. Let’s say we have the following command await Promise.all([ server1.logMessage(), server2.logMessage() ]) And let’s say server1 takes 2secs to log and server2 takes 5secs to log. And what happens if the server1.logMessage() returns a error in 1 sec. Can we be sure that server2 will at… Read More How does promise.all handle multiple API calls when one of them fails?