This question already has answers here:
NodeJS Timeout a Promise if failed to complete in time
(8 answers)
Closed 2 years ago.
I am waiting for my promise to resolve in an async function using await but I want to wait for only 20 sec. If no response comes in that time (neither positive nor negative from the promise), I want to continue and display 'timeout'. How can I do this?
You can use Promise.race:
const promise1 = func();
const promise2 = new Promise((res, rej) => setTimeout(rej, 20000));
try {
await Promise.race([promise1, promise2]);
} catch (e) {
// time out or func failed
}
Related
This question already has answers here:
Why does nodejs stop execution of while true loop inside async function
(3 answers)
Closed 28 days ago.
I have a sample code on TS playground represents my problem.
In an async function, I log the result after await for the promise, but only the code inside promise run, not the log outside of it. Could someone explain this problem?
Here is the code:
const asyncFnc = async () => {
let result = false;
await new Promise(resolve => {
setTimeout(() => {
// This log worked
console.log('waited 5s');
result = true;
}, 5000);
});
// This log did not worked
console.log(result);
}
asyncFnc();
And the result:
await sends the parent function to sleep until the promise on the right hand side settles (i.e. resolves or rejects).
Your promise never resolves or rejects. (i.e. you don't call resolve, make use of the second argument, or throw an exception).
Thus the parent function sleeps forever.
The idiomatic way to write this would be to avoid setting variables in the wider scope as a side effect, and just resolve with the values instead.
const asyncFnc = async () => {
const result = await new Promise(resolve => {
setTimeout(() => {
console.log('waited 5s');
resolve(true);
}, 5000);
});
console.log(result);
}
asyncFnc();
You need to call resolve() in your timeout
This question already has answers here:
Why do I need to await an async function when it is not supposedly returning a Promise?
(3 answers)
async/await implicitly returns promise?
(5 answers)
async function - await not waiting for promise
(5 answers)
async/await always returns promise
(4 answers)
Closed 5 months ago.
Javascript (or Node.js) is not returning objects as expected.
The following code logs an empty array.
const _sodium = require('libsodium-wrappers');
const sodium = (async () => {
await _sodium.ready;
return _sodium;
})();
console.log(Object.getOwnPropertyNames(sodium));
On the other hand, the following code works as expected.
const _sodium = require('libsodium-wrappers');
(async () => {
await _sodium.ready;
const sodium = _sodium;
console.log(Object.getOwnPropertyNames(sodium));
})();
In the first code snippet, it seems like the sodium object is tied to its lexical environment, which would explain why an empty array is printed. I would be interested in what's going on here (including an explanation of what's going on under the hood). Thanks.
It's not about lexical context.
You just not awaiting for ready in the first example.
First example simplified:
(async() => {
console.log('start')
await new Promise(r => setTimeout(r, 3000))
console.log('finish')
})()
Second example simplified:
console.log('start');
(async() => {
await new Promise(r => setTimeout(r, 3000))
})();
console.log('finish')
This question already has answers here:
Resolve promises one after another (i.e. in sequence)?
(36 answers)
Is Node.js native Promise.all processing in parallel or sequentially?
(14 answers)
Closed 6 months ago.
I asked a question that was closed due to similar questions but even after reading those and seeing that map is not Promise aware, I have tried to use await Promise.all() around my mapping.
let foo = [1,2,3];
(async() => {
await Promise.all(foo.map(x => {
return new Promise(async function(resolve) {
await setTimeout(() => {
console.log("X")
resolve()
}, 3000)
});
}))
})()
instead of sequentially getting a "X" log every 3 seconds, it's just waiting 3 seconds and then logging them all. I am not sure what I am doing wrong at this point.
You are setting all the timeouts together at once. You need to increase the timeout duration in each iteration, maybe like this:
let foo = [1,2,3];
(async() => {
await Promise.all(foo.map((x, i) => {
return new Promise(async function(resolve) {
await setTimeout(() => {
console.log("X")
resolve()
}, 3000 * (i + 1))
});
}))
})()
This question already has answers here:
When is the body of a Promise executed?
(4 answers)
Why do promises execute at the point of declaration?
(2 answers)
Why does my Promise definition gets executed?
(2 answers)
Is the Promise constructor callback executed asynchronously?
(2 answers)
Closed 8 months ago.
I can't get why function in new Promise fires immediately. Shouldn't it be put in Microtask queue and fire after all global code has executed?
Code:
const prom = new Promise((resolve, reject) => {
console.log("in Promise");
resolve(20);
});
prom.then((data) => {
console.log(12312323);
console.log(data);
});
console.log(77);
Link to code
Output:
in Promise
77
12312323
20
When you construct a Promise, its task begins execution immediately.
const p = new Promise((resolve) => {
console.log('in promise');
resolve("value");
})
Promises are primarily useful for asynchronous tasks like network requests, and their delayed resolution can make it seem like the execution itself has been deferred, but that's not what's happening.
In this example you can see that there can be a significant delay between when the task starts and when the promise is resolved:
const p = new Promise((resolve) => {
console.log('first: in promise'); // first
setTimeout(() => resolve('resolved value'), 2000) // wait 2 seconds before resolving.
})
p.then(val => console.log(`third: ${val}`)); // third
console.log("second: after p.then"); // second
This question already has answers here:
Promise override causes then calls to error
(1 answer)
Extend Javascript promise and resolve or reject it inside constructor
(5 answers)
Closed 12 months ago.
class ControllablePromise extends Promise {
constructor() {
let resolveClosure = null;
let rejectClosure = null;
super((resolve, reject) => {
resolveClosure = resolve;
rejectClosure = reject;
});
this.resolveClosure = resolveClosure;
this.rejectClosure = rejectClosure;
}
}
const prom = new ControllablePromise();
async function func(){
try{
await prom;
console.log('done');
}
catch(err){
console.log(err.message);
}
}
setTimeout(() => {prom.rejectClosure(); console.log('resolved');}, 1000);
func();
I am attempting to create a promise that I can resolve/reject externally. I can do this functionally without issue, just returning a regular promise after I attach the resolve/reject closures to it, but when attempting to turn it into a class I am running into difficulty. The code above throws an error when attempting to await the promise. Why is this happening exactly?