简体   繁体   English

Node.js Array.map() 是异步的吗?

[英]Is Node.js Array.map() asynchronous?

Can I count on nodeIDs mapping is completed every time doSomething() is called?我可以指望每次调用 doSomething() 时都完成 nodeIDs 映射吗?

nodeIDs = $.map(nodeIDs, function(n){
    return n.match(/\d+$/);
});
doSomething(nodeIDs);

I thought all callbacks in node.js are asynchronous?我认为 node.js 中的所有回调都是异步的? I did read an article on general programming that callback could be synchronous but I am not sure about node.js?我确实读过一篇关于一般编程的文章,回调可以是同步的,但我不确定 node.js?

JavaScript is also a functional programming language. JavaScript 也是一种函数式编程语言。 What you have here is a «higher order function», a function which takes a function as a parameter.你在这里拥有的是一个“高阶函数”,一个将函数作为参数的函数。 Higher order functions are synchronous (but see note below).高阶函数是同步的(但请参见下面的注释)。

Sources:资料来源:

map() is a typical example of a higher order function. map()是高阶函数的典型例子。 It takes a function and applies it to all elements of an array.它接受一个函数并将其应用于数组的所有元素。 The definition sounds very «functional».这个定义听起来很“功能性”。 This function is also not provided by Node. Node.js 也没有提供这个功能。 It is documented by MDN Array.prototype.map() and specified by ECMAScript 5.1 .它由MDN Array.prototype.map() 记录并由ECMAScript 5.1指定。

To answer your question: Yes, doSomething(nodeIDs) is called after all elements have been applied.回答您的问题:是的,在应用所有元素调用doSomething(nodeIDs)


Note: The higher order function is a concept of functional programming. 注意:高阶函数是函数式编程的一个概念。 JavaScript is functional, but also deeply seated in the practicality of executing code inside a browser or on the server. JavaScript 是功能性的,但也深深植根于在浏览器内或服务器上执行代码的实用性。 I would say that for example setTimeout() is not a higher order function even if it takes a function as a parameter because setTimeout() is not really purely functional because it uses time. 我会说,例如setTimeout()不是高阶函数,即使它接受一个函数作为参数,因为setTimeout()并不是真正的纯函数,因为它使用时间。 Pure functionality is timeless. 纯粹的功能是永恒的。 For example the result of map() doesn't depend on time. 例如map()的结果不依赖于时间。 And that's what this question is really about. 这就是这个问题的真正意义所在。 If something doesn't depend on time you execute it synchronously. 如果某些内容不依赖于时间,则同步执行它。 Problem solved. 问题解决了。

Thanks to Simon for challenging the definition of the higher order function in JavaScript.感谢 Simon 挑战 JavaScript 中高阶函数的定义。

Yes, .map is synchronous.是的, .map是同步的。 "Callback" does not imply "asynchronous". “回调”并不意味着“异步”。

import the async module to have an asynchronous ' map ' method导入async模块以具有异步“ map ”方法

var async = require('async');

var arr = ['1','2'];
async.map(arr, getInfo, function (e, r) {
  console.log(r);
});

function getInfo(name, callback) {
  setTimeout(function() {
    callback(null, name + 'new');
  }, 1000);
}

This function is synchronous - otherwise it couldn't return the result of the map operation.此函数是同步的 - 否则无法返回映射操作的结果。

Any callbacks that might take longer time (mainly due to IO) are asynchronous in nodejs - unless the method is explicitely marked as being synchronous (such as fs.readFileSync - but that doesn't use a callback).任何可能需要更长时间(主要是由于 IO)的回调在 nodejs 中都是异步的 - 除非该方法被明确标记为同步(例如fs.readFileSync - 但不使用回调)。 You probably confused that somehow.你可能以某种方式混淆了这一点。

use a forof (is synchronous):使用 forof(同步):

let arr = ['fizz', 'buzz']
//example
    for (const item of arr) {
   //this Examp_func returns array
     console.log((await Examp_func(item )).length);
    }

There is useful lib awaiting .有有用的 lib等待 And map will help you.地图会帮助你。

YES, map function is asynchronous and is part of ECMAScript .是的, map函数是异步的,是ECMAScript 的一部分。 NodeJS uses map from that definition. NodeJS 使用该定义中的 map。 Try running this code to see it in action.尝试运行此代码以查看其运行情况。

 const array = ['first','second'] function name(params) { setTimeout( () => console.log('name: ' + params), 5000 ); } console.log('wait for 5 seconds') array.map(v => { name(v) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM