Jaa*_*and 23 javascript angularjs
有时我需要等待.forEach()方法完成,主要是在'loader'函数上.这就是我这样做的方式:
$q.when(array.forEach(function(item){
//iterate on something
})).then(function(){
//continue with processing
});
Run Code Online (Sandbox Code Playgroud)
我不禁觉得这不是等待.forEach()完成的最好方法.做这个的最好方式是什么?
小智 38
var foo = [1,2,3,4,5,6,7,8,9,10];
Run Code Online (Sandbox Code Playgroud)
如果你实际上是在循环中做异步的东西,你可以把它包装在一个承诺中......
var bar = new Promise((resolve, reject) => {
foo.forEach((value, index, array) => {
console.log(value);
if (index === array.length -1) resolve();
});
});
bar.then(() => {
console.log('All done!');
});
Run Code Online (Sandbox Code Playgroud)
Aqe*_*eel 14
使用for of而不是forEach. 像这样:
for (const item of array) {
//do something
}
console.log("finished");
Run Code Online (Sandbox Code Playgroud)
finished完成循环后将记录“ ”。
Dou*_*ank 12
使用ES6进行这项工作的最快方法就是使用for..of循环。
const myAsyncLoopFunction = async (array) {
const allAsyncResults = []
for (const item of array) {
const asnycResult = await asyncFunction(item)
allAsyncResults.push(asyncResult)
}
return allAsyncResults
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用以下方法并行遍历所有这些异步请求Promise.all():
const myAsyncLoopFunction = async (array) {
const promises = array.map(asyncFunction)
await Promise.all(promises)
console.log(`All async tasks complete!`)
}
Run Code Online (Sandbox Code Playgroud)
Ism*_*OUH 10
forEach 不是异步的,例如在这段代码中:
array.forEach(function(item){
//iterate on something
});
alert("Foreach DONE !");
Run Code Online (Sandbox Code Playgroud)
forEach完成后你会看到警报.
小智 8
forEach() 不返回任何内容,因此更好的做法是map()+Promise.all()
var arr = [1, 2, 3, 4, 5, 6]
var doublify = (ele) => {
return new Promise((res, rej) => {
setTimeout(() => {
res(ele * 2)
}, Math.random() ); // Math.random returns a random number from 0~1
})
}
var promises = arr.map(async (ele) => {
// do some operation on ele
// ex: var result = await some_async_function_that_return_a_promise(ele)
// In the below I use doublify() to be such an async function
var result = await doublify(ele)
return new Promise((res, rej) => {res(result)})
})
Promise.all(promises)
.then((results) => {
// do what you want on the results
console.log(results)
})
Run Code Online (Sandbox Code Playgroud)
如果您在循环中有一个异步任务并且您想等待。您可以使用for await
for await (const i of images) {
let img = await uploadDoc(i);
};
let x = 10; //this executes after
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23255 次 |
| 最近记录: |