admin管理员组文章数量:1357712
I want to pass each item into a function that take times. But seems that the JS function is asynchronized. How can I call the function sequentially ? (Pass next item to function after the previous done)
function main() {
for (var i = 0; i < n ; i++) {
doSomething(myArray[i]);
}
}
function doSomething(item) {
// do something take time
}
My solution is call the function recusively. But I want to know is there a different way to solve this issue ? Thanks.
function main() {
doSomething(myArray, 0);
}
function doSomething(item, i) {
// do something take time
doSomething(myArray, i + 1);
}
I want to pass each item into a function that take times. But seems that the JS function is asynchronized. How can I call the function sequentially ? (Pass next item to function after the previous done)
function main() {
for (var i = 0; i < n ; i++) {
doSomething(myArray[i]);
}
}
function doSomething(item) {
// do something take time
}
My solution is call the function recusively. But I want to know is there a different way to solve this issue ? Thanks.
function main() {
doSomething(myArray, 0);
}
function doSomething(item, i) {
// do something take time
doSomething(myArray, i + 1);
}
Share
Improve this question
edited Jul 7, 2016 at 10:31
Aaron
asked Jul 7, 2016 at 2:12
AaronAaron
831 silver badge7 bronze badges
6
- As far as I know, for-cycles and function calls in javascript are synchronous, so it must be something else that's making the function async. Maybe it's something inside the function itself. – Andros Rex Commented Jul 7, 2016 at 2:19
- Sorry for not mention that, my code is running on a node.js server. Is that why the function is async ? – Aaron Commented Jul 7, 2016 at 2:22
-
1
myArray.forEach(doSomething)
es to mind. – Malk Commented Jul 7, 2016 at 2:22 - Functions don't bee async just because they're on node.js. What does your function actually do? Asynchronous functions usually have a callback facility or return a promise, so you can use that to coordinate the subsequent call. – nnnnnn Commented Jul 7, 2016 at 2:26
- Hi @nnnnnn, the function do something like http post request, and i have a callback function. My solution is put the doSomething(myArray, i + 1); inside the callback function. – Aaron Commented Jul 7, 2016 at 2:31
2 Answers
Reset to default 6In JavaScript, as of 2020, the for-loop is async/await aware. You can return a promise and then await
that promise inside of the for loop. This causes the loop to execute in a series, even for long running operations.
function randomSleep(ms, seed=10) {
ms = ms * Math.random() * seed;
return new Promise((resolve, reject)=>setTimeout(resolve, ms));
}
async function doSomething(idx) {
// long running operations
const oute = await randomSleep(500);
return idx;
}
const arrItems = ['a','b','c','d'];
for(let i=0,len=arrItems.length;i<len;i++) {
const result = await doSomething(i);
console.log("result is", result)
}
Read more about async await in for loops and forEach loops here https://zellwk./blog/async-await-in-loops/
if you want to pass next item to function after the previous done, you can try to use promise, just like this
var Q = require('q');
var promise;
main();
function main() {
promise = doSomethingPromise(0)
for (var i = 1; i < 10 ; i++) {
(function (i) {
promise = promise.then(function (res) {
return doSomethingPromise(res + ' ' + i)
});
})(i)
}
}
function doSomethingPromise (item) {
var d = Q.defer();
d.resolve(doSomething(item));
return d.promise;
}
function doSomething(item) {
// do something take time
console.log('doSomething', item);
return item;
}
it can make you function to be called by order.
本文标签: nodejsHow to call a JavaScript function sequentially in a for loopStack Overflow
版权声明:本文标题:node.js - How to call a JavaScript function sequentially in a for loop? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744079958a2587446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论