.then Of Promise.all Result Never Executes
I am trying to use Promise.all and map instead of the forEach loop so the task can be asynchronous. All of the promises in the Promise.all array get executed and are resolved. The
Solution 1:
You have code paths that don't resolve, specifically:
if(item.address == "") {
/*if(!item.picURL) {
item.picURL = 'assets/blankprof.png';
}*/
//arr.push({'pic':item.picURL, 'salon':item.username, 'distance':"No Address"});
//x++;
}
Solution 2:
It is very easy getting into deadlocks like @andy-gaskell pointed out when creating new Promises. To avoid that you better do one of these; All of these may throw an impossible error or return 1.
functionpromiseWithTryCatch() {
returnnewPromise((resolve, reject) => {
try {
let result;
// your coderesolve(result);
}
catch(ex) {
reject(ex);
}
})
}
functionpromiseWithResolve() {
returnPromise.resolve()
.then(() => {
let result;
// your codereturn result;
})
}
asyncfunctionpromiseWithAsync() {
let result;
// your codereturn result;
}
Replace the comment with your code and place the ending result into the variable result. If your code contains async code, you better make a new function with the same pattern and return that as the result, eg: result = [promise method]
- The most scary one is "new Promise", if any code does not resolve or reject it will end in a "dead-lock", that's why it is important to have a try/catch.
- If you are using Promise.resolve() it will catch any errors inside .then but don't do any code outside .then, otherwise you have to catch exception and return a Promise.reject(new Error())
- The safest is async function, since any "throw" will return a Promise.reject, and "return" will return a Promise.resolve
Play around with this example, but I had to comment out async/await since this snippet tool doesn't handle ES2016.
functionnestedPromise(num) {
returnPromise.resolve(' myNestedValue is ' + num);
}
functionpromiseWithTryCatch() {
returnnewPromise((resolve, reject) => {
try {
let mynumber = 2 + 5;
nestedPromise(mynumber)
.then((answer) => {
resolve(answer.trim());
})
.catch(ex => {
// error handling for async-codereject(ex);
})
}
catch(ex) {
// error handling for sync-codereject(ex);
}
})
}
functionpromiseWithResolve() {
returnPromise.resolve()
.then(() => {
let mynumber = 2 + 5;
returnnestedPromise(mynumber);
})
.then((answer) => {
// do something with the answerreturn answer.trim();
})
}
/*
async function promiseWithAsync() {
let mynumber = 2 + 5;
let answer = await nestedPromise(mynumber);
return answer.trim();
}
*/promiseWithTryCatch()
.then(answer =>console.log('promiseWithTryCatch result is ' + answer))
.catch(err =>console.log('promiseWithAsync error is ' + err.message));
promiseWithResolve()
.then(answer =>console.log('promiseWithResolve result is ' + answer))
.catch(err =>console.log('promiseWithResolve error is ' + err.message));
/*
promiseWithAsync()
.then(answer => console.log('promiseWithAsync result is' + answer))
.catch(err => console.log('promiseWithAsync error is ' + err.message));
*/
Post a Comment for ".then Of Promise.all Result Never Executes"