Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 13 days ago.
Improve this question
Im currently trying to make a timeout for a function
setTimeout(() => function_name(), 10000)
This function may raise an exception or reject a promise
How do I handle a rejection in this case ?
Im unsure of what to try
Just wrap it in a try/catch
setTimeout(() => {
try {
function_name();
} catch (err) {
console.log(err);
}
}, 10000);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
I encountered a strange behavior when I defined 4 Promises which the last one is described as just rejected and when I tried to run each promise separately, the last one is fired! This will fire rejection especially when I want to run all Promises (_1 _2 _3) in Promise.all().
I wanted to use catch() for promise_2 in Promise.all([promise.catch((err)=>console.log(err))]).then(...) from handling the error happend in promise_2 without cancelling the operation of other Promises
Thx
const asyncFunction=(timer,callback)=>{
setTimeout(() => {
callback(10)
}, timer);
}
const promise_1=new Promise((resolved,reject)=>{
const date=new Date().getSeconds()
date%2===0?asyncFunction(1000,(value)=>resolved(value)):reject(new Error("hello"))
})
const promise_2=new Promise((resolved,reject)=>{
asyncFunction(2000,(value)=>resolved(value))
})
const promise_3=new Promise((resolved,reject)=>{
asyncFunction(3000,(value)=>resolved(value))
})
const promise_4=new Promise((resolved,reject)=>{
reject(new Error("i am bad"))
})
promise_1.then((value)=>console.log(value))
first in Node
C:\Program Files\nodejs\node.exe .\test.js
Process exited with code 1
Uncaught Error Error: I am bad<--------------------------what is it?this is for promise_4 ,while I didn't call it
second in V8
See here
[in V81
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I saw a piece of code that looks funny to me. It feels like there are multiple things wrong here. Am I seeing a ternary here returning an error rather than a value? Shouldn't this therefore be an if-else?
const aData = await response.json();
return await new Promise((resolve, reject) => {
(aData.title === aName)
? resolve('A data was found')
: reject(new Error('Incorrect data was returned'));
});
Both resolve and reject just return the value undefined, and your callback function doesn't return anything at all. Using a ternary operator to make it a single expression is rather useless.
For clarity, you should better write
if (aData.title === aName) resolve('A data was found');
else reject(new Error('Incorrect data was returned'));
although really you shouldn't be using the new Promise constructor here at all. Just
const aData = await response.json();
if (aData.title !== aName) throw new Error('Incorrect data was returned');
else return 'A data was found';
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I know my code has the correct logic, but everywhere I look I'm told not to use nested promises. Is this a use case for nested promises?
The logic is as follows:
promise1 fails -> reverse promise1
promise1 succeeds -> promise2/promise3 fails -> reverse promise1,
promise2, promise 3
promise1 succeeds -> promise2 & promise3 succeeds
let data = null;
promise1.then((response) => {
data = response;
return Promise.all([promise2(), proimse3()])
.catch((error) => {
//Reverse only promise2, promise3
//Throw error to reverse promise1
});
}).then((id) => {
something(data);
}).catch((error) => {
//Reverse only promise1
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I access to the user object in the promises callback ?
I try to bind it with no success.
const promises = User.find(req.user._id).exec();
promises.then(function (user) {
noAccessFunction().then(function (customer) {
// HERE
user.save();
});
});
Promises are not callbacks! Mutate it as if it were a chain of values. Maybe something like this:
const promises = User.find(req.user._id).exec();
promises.then(function (user) {
return myfunction().then(function (customer) {
return user.save();
});
});
Promises can also be flattened:
const promises = User.find(req.user._id).exec();
promises.then(function (user) {
return myfunction(user);
})
.then(function (customer) {
return customer.save();
});
In any case the code above is speculation since you have not provided enough information as to what you want to happen.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Using ES6 syntax to define a class and its methods, how can I define a method as a Promise, without having to wrap it in a regular function that returns a promise? I want to do something like the following below:
class PromiseMethod {
promiseMethod = new Promise(function(resolve, reject) {
return resolve();
}
}
Promises are just an object returned from a function — whether it be a method or not doesn't matter. Try this:
class Something {
promiseMethod () {
return new Promise(...);
}
}
(new Something).promiseMethod().then(...)
But maybe you wanted to not have to call the method and be able to use the Promise methods directly? In this case, it's not a method, it's a property:
class Something {
constructor () {
this.promiseProperty = new Promise(...);
}
}
(new Something).promiseProperty.then(...);