How check my state value before call a function?

I have a big problem. I want set some of value in my cookie. but before set in cookie I should encrypt my value and set them in cookie. first my states are undefined and when I navigate to next page those set undefined and made some problems in getting cookies values. how can I… Read More How check my state value before call a function?

javascript/nodejs – async code inside while loop which is inside a map array method

I am executing the code block below. For the jsfiddle line, click here async function sleep(time = 1) { const sleepMilliseconds = time * 1000 return new Promise(resolve => { setTimeout(() => { resolve(`Slept for: ${sleepMilliseconds}ms`) }, sleepMilliseconds) }) } async function main() { let a = [1,2] a.map(async(el)=>{ let i = 3 console.log(el,i) while(i>0)… Read More javascript/nodejs – async code inside while loop which is inside a map array method

Why does the console.log output during instantiation of a Promise object, but does not output when a ".then()" is called on it?

const promise = new Promise((resolve, reject) => { console.log(“working 1.0”) setTimeout(() => { console.log(“working 2.0”) resolve([90, 70, 55]); }, 7000); }); console.log(“Hi”) promise.then(values => { console.log(values[1]); }); This is the output that I get: working 1.0 Hi working 2.0 70 But why isn’t it: working 1.0 Hi working 1.0 working 2.0 70 My idea being,… Read More Why does the console.log output during instantiation of a Promise object, but does not output when a ".then()" is called on it?

Error with javascript promise and then condition

I’m learning Javascript promises and thens, and am confused with this error using Node.js. I would like dostart() to wait until nonblocking sleep is finished, and then return "Resolved" to the main func when it is done. I get this error: dostart().then(value => { ^ TypeError: Cannot read properties of undefined (reading ‘then’) Help appreciated… Read More Error with javascript promise and then condition

Promises and Async/Await don't seem to do anything in Express/node

I’m learning node and express and I’m trying to implement a function that retrieves data from a csv file uploaded by the user. The data should be processed first and then outputted to the console, but it instead outputs an empty array before processing it. I followed examples of async/await from tutorials I found online… Read More Promises and Async/Await don't seem to do anything in Express/node

why does void inside of promise.then not return undefined?

My co-worker wrote structurally this kind of code: Promise.resolve(2).then(void console.log(‘3’)).then(x => x + 2) Can someone explain why the x argument is not "undefined" in the last then >Solution : Syntax then(onFulfilled) then(onFulfilled, onRejected) Parameters onFulfilled (Optional) A Function asynchronously called if the Promise is fulfilled. This function has one parameter, the fulfillment value. If… Read More why does void inside of promise.then not return undefined?

Why my async function doesn't await to the Promise in JavaScript?

My code is the following: const match = async (tender, searchCriteria) => { const tenderAccount = await tenderAccountController.findOrCreate( tender, searchCriteria, ); if (searchCriteria.emailFrequency !== ‘real-time’) { return null; } // Notify Stakeholders await notifyStakeholders(tender, tenderAccount, searchCriteria); }; This function "match" is called inside a for loop [for (const searchCriteria of searchCriterias) {}] findOrCreate function is… Read More Why my async function doesn't await to the Promise in JavaScript?

Create promised function to sort by name and then chain it

Create another promisified function that sorts this employee list from below response by name. Chain it to below promise function arr() { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(employeeDetailsArray); }, 2000); }); } arr() .then(function(result) { console.log(result); return arr(); }) .then() ////code to sort by name, output should be sorted list of employee details… Read More Create promised function to sort by name and then chain it

How to return a component in an async method withing a FlatList

I have a functional component const Foo = () => { const _renderSomething = async id => { const data = await database.get(SOME_TABLE).Where(someColumnValue = id); return Promise.resolve( <AnotherComponent data={data} /> ); }; const _renderCard = ({item}) => { const {code, id} = item; … return ( <Card index={code}> {_renderSomething(id)} </Card> ); }; return ( <FlatList… Read More How to return a component in an async method withing a FlatList