Why Node.js does not wait for promise to resolve before exiting?

Advertisements When I execute the following code, I thought Node.js will wait till we call the resolve. Till then the myPromise will be in the <pending> state. Then how come node exits before it resolves? The following code exits immediately! const myPromise = new Promise(resolve => { // nothing doing with the resolve }); myPromise… Read More Why Node.js does not wait for promise to resolve before exiting?

why .then() is getting called twice in promises {javascript}

Advertisements Problem: last 2 .then are getting called even while i am returning Promise.reject(). 1st .then() is printing undefined and 2nd .then() is also printing the output can someone please explain why? let p4 = new Promise((resolve,reject)=>{ resolve("p4 Resolved"); }); p4.then((val)=>{ console.log(val); return Promise.reject("20 Rejected"); } ) .catch((error)=>{console.log(error)}) .then((val)=>{console.log(val);return 290}) .then((val)=>{console.log("Last then : "+val);}) ;… Read More why .then() is getting called twice in promises {javascript}

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

Advertisements 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)… 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?

Advertisements 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… 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

Advertisements 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… Read More Error with javascript promise and then condition

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

Advertisements 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… 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?

Advertisements 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.… Read More why does void inside of promise.then not return undefined?