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
});
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 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);
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 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 2 years ago.
Improve this question
I am trying to understand the below code/syntax which uses await Promise.all;
How is the array getting assigned from the endpoint response ?
const getState = async (code) => {
try {
const [
{data: dataResponse},
{data: stateDistrictWiseResponse},
{data: statesDailyResponse},
{data: stateTestResponse},
{data: sourcesResponse},
{data: zonesResponse},
] = await Promise.all([
axios.get('https://example.org/data.json'),
axios.get('https://example.org/state_district_wise.json'),
axios.get('https://example.org/states_daily.json'),
axios.get('https://example.org/state_test_data.json'),
axios.get('https://example.org/sources_list.json'),
axios.get('https://example.org/zones.json'),
]);
console.log(stateTestResponse.states_tested_data);
}
}
Promise.all converts an array of promises into an array of results.
axios.get returns a promise that, when resolved, returns an object with the shape { data: <result goes here>
Your code is destructuring those objects within the returned array.
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 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.