Relative Content

Tag Archive for javascriptpromise

Javascript Promises: Why does resolve variable need to be accepted in the Promise ()?

function resolveAfter2Seconds() { return new Promise((resolve) => { setTimeout(() => { resolve(‘resolved’); }, 2000); }); } async function asyncCall() { console.log(‘calling’); const result = await resolveAfter2Seconds(); console.log(result); // Expected output: “resolved” } asyncCall(); My question is on the return new Promise((resolve) => { line. Why does resolve need to be passed into the Promise? What […]

Javascript Promise.race

how does the promises(p1, p2, p3) gets executed differently in the getStates() function outside the setTimeout and inside the setTimeout

can there be a promise in pending state when its [[PromiseState]] value is “rejected”?

let b=Promise.reject(2).then(()=>{return Promise.resolve(2)}) console.log(b) the output of this code is Promise {<pending>} [[Prototype]]: Promise [[PromiseState]]: “rejected” [[PromiseResult]]: 2 how can promise be in pending state when [[PromiseState]] is “rejected” state like above. shouldn’t output be like Promise {: 2} also why the whole code runs first then this code throws error when Promise.reject() is encountered? […]

Timeout not resetting for each Promise

I am trying to make it so each mapbox cell will download. I want it so every loop has 60 seconds to download the street and satelite maps. This seems to timeout every 60 seconds regardless of whether it is successfully constantly looping and downloading maps.
So it will loop and download correctly but timeout after 60 seconds every time, instead of first loop, allow 60 seconds, finishes, second loop restart and allow 60 seconds, finishes….
They take less than 10 seconds to download so it should never timeout. I am also logging to see them finish.

How is the order of Promise’s Handler (then – catch – finally) push into MicroTask queue in Javascript?

Case 1 new Promise((resolve, reject) => { resolve(1); }) .then((result) => console.log(result)) .catch((error) => console.log(error)) new Promise((resolve, reject) => { reject(‘Error!’); }) .then((result) => console.log(result)) .catch((error) => console.log(error)) new Promise((resolve, reject) => { resolve(3); }) .then((result) => console.log(result)) .catch((error) => console.log(error)) Result of expect: 1 – Error! – 3 Result of actual: 1 – 3 […]