使用回调在数组上执行函数 [英] executing function on array using callbacks
问题描述
使用回调函数调用对象数组上的相同函数的正确方法是什么?
基本上按顺序处理异步调用。
doAsynchFunction(data,callback){
console.log(data);
wait(1000,callback); //做一些需要一些时间并执行回调的事情
}
var a = [1,2,3,4,5,6];
我想看到这些数字相隔1秒。
<您可以使用
Promise.all()
来处理异步过程,其中结果可以以任何顺序返回
var queue = [0,1,2,3,4,5,6,7];函数asyncFn(n){return new Promise(function(resolve){setTimeout(function(){console.log(processing:,n)resolve(n)},Math.random()* 3000)})} Promise.all (value){return asyncFn(value)}))。然后(function(results){console.log(results)})catch(function(err){console.log(err)})
或使用队列按顺序处理异步函数
var queue = [0,1,2,3,4,5,6,7],res = [],queueCopy = queue.slice(0); function asyncFn (n){return new Promise(function(resolve){setTimeout(function(){console.log(processing:,n)resolve(n)},Math.random()* 3000)})} function processQueue arr){return asyncFn(arr.shift()).then(function(result){res.push(result)if(arr.length){return processQueue(arr)} else {return res}})} processQueue ).then(function(results){console.log(results)})。catch(function(err){console.log(err)})
>
在更新时调整 js
利用 setTimeout()
, Function.prototype.bind()
将参数传递给 setTimeout
。注意,回调
在此实例中将是 doAsynchFunction
本身。
var a = [1,2,3,4,5,6],aCopy = a.slice(0); function wait(duration,callback){setTimeout(function(){callback )},duration)} function doAsynchFunction(arr,cb){console.log(arr.shift()); //通过`arr`和`cb`:`doAsynchFunction` to` wait` if(arr.length)wait(1000,cb.bind(null,arr,cb)); } doAsynchFunction(aCopy,doAsynchFunction);
What is the correct way to call the same function on an array of objects using callbacks to advance?
Essentially processing asynchronous calls sequentially.
doAsynchFunction(data,callback){
console.log(data);
wait(1000,callback); // do something that takes some time and execute callback
}
var a=[1,2,3,4,5,6];
I'd like to see the numbers appear 1 second apart
You could use Promise.all()
to process asynchronous processes where the result could be returned in any order
var queue = [0, 1, 2, 3, 4, 5, 6, 7];
function asyncFn(n) {
return new Promise(function(resolve) {
setTimeout(function() {
console.log("processing:", n)
resolve(n)
}, Math.random() * 3000)
})
}
Promise.all(queue.map(function(value) {
return asyncFn(value)
}))
.then(function(results) {
console.log(results)
})
.catch(function(err) {
console.log(err)
})
or use a queue to process asynchronous functions in sequential order
var queue = [0, 1, 2, 3, 4, 5, 6, 7]
, res = []
, queueCopy = queue.slice(0);
function asyncFn(n) {
return new Promise(function(resolve) {
setTimeout(function() {
console.log("processing:", n)
resolve(n)
}, Math.random() * 3000)
})
}
function processQueue(arr) {
return asyncFn(arr.shift())
.then(function(result) {
res.push(result)
if (arr.length) {
return processQueue(arr)
} else {
return res
}
})
}
processQueue(queueCopy)
.then(function(results) {
console.log(results)
})
.catch(function(err) {
console.log(err)
})
Adjusting js
at updated Question to utilizing setTimeout()
, Function.prototype.bind()
to pass function reference with parameters to setTimeout
. Note, callback
in this instance would be doAsynchFunction
itself.
var a = [1, 2, 3, 4, 5, 6], aCopy = a.slice(0);
function wait(duration, callback) {
setTimeout(function() {
callback()
}, duration)
}
function doAsynchFunction(arr, cb) {
console.log(arr.shift());
// pass `arr`, and `cb` : `doAsynchFunction` to `wait`
if (arr.length) wait(1000, cb.bind(null, arr, cb));
}
doAsynchFunction(aCopy, doAsynchFunction);
这篇关于使用回调在数组上执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!