使用回调在数组上执行函数

编程入门 行业动态 更新时间:2024-10-27 00:27:23
本文介绍了使用回调在数组上执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用回调函数调用对象数组上的相同函数的正确方法是什么?

基本上按顺序处理异步调用。

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);

更多推荐

使用回调在数组上执行函数

本文发布于:2023-07-20 17:42:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   回调   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!