node .js 同步_Node.js中的同步执行

node .js 同步

Everyone loves NodeJS in part because it embraces a non-blocking philosophy;  interactions are asynchronous and thus, theoretically, allow for faster all-around processing.  When creating a simple script for my upcoming redesign, I found the asynchronocity ... annoying.  I was quickly sinking into callback hell with the server executions I was running through Node.js.  Luckily I found a great package called exec-sync which allows synchronous execution of shell commands so that I don't find myself many callbacks deep.

每个人都喜欢NodeJS,部分原因是它拥护无阻塞的哲学。 交互是异步的,因此从理论上讲,它允许更快的全方位处理。 当为我即将进行的重新设计创建简单的脚本时,我发现异步性……很烦人。 我通过Node.js运行的服务器执行Swift陷入回调地狱。 幸运的是,我找到了一个名为exec-sync的好程序包,该程序包允许同步执行Shell命令,这样我就不会发现很多回调。

安装执行同步 (Installing exec-sync)

The exec-sync package is available via npm:

exec-sync软件包可通过npm获得:


npm install exec-sync


Don't you love package management?!

您不喜欢包裹管理吗?

使用execSync (Using execSync)

exec-synce works like any other Node.js component;  require it and it's ready to use:

exec-synce的工作方式与任何其他Node.js组件相同; 需要它并可以使用:


// Require the dependency
var execSync = require("exec-sync");

// The traditional way
var exec = require('child_process').exec;
exec('ORIGINAL COMMAND', function(err, stdout, stderr) {
	// React to callback

	exec('SOME OTHER COMMAND', function(err2, stdout2, stderr2) {
		// More reacting

		// ... more nesting that isn't desired
	});
});

// The execSync way

var execSync = require("exec-sync");

// Create the JavaScript dir, uglify contents
execSync("cp -R "  + sourceDir + "js " + jsDir);
doCompress && walkAndDo("js", jsDir, function(fileName, stat) {
	execSync("./node_modules/uglify-js/bin/uglifyjs -nc --reserved-names 'require,define,curl,z' --unsafe --lift-vars --overwrite " + fileName);
});

// Copy the "crons" and "cache" directories over
execSync("cp -R " + sourceDir + "crons " + dirPath + "crons");
execSync("cp -R " + sourceDir + "cache " + dirPath + "cache");

// Delete all bullshit ".DS_Store" files
execSync("find . -type f -name .DS_Store -exec rm -rf {} +");

// And when all is done, copy to another place
execSync("mv " + dirPath + " ../../../wp-content/themes/jack"); 


Note that instead of passing a callback to the execSync method, I can simply assume things are running from the top down, allowing me to avoid a bunch of nested callbacks that would only muddy up my code.  Of course some processes could be asynchronous but exec-sync allows me to keep a top-down thought process. Node.js does have utilities for a few of these functions, but running shell keeps the code shorter.

请注意,除了将回调传递给execSync方法外,我还可以简单地假设事情是从上到下运行的,这使我避免了一堆嵌套的回调,这些回调只会使我的代码混乱。 当然,某些过程可能是异步的,但是exec-sync使我能够保持自上而下的思想过程。 Node.js确实具有用于其中一些功能的实用程序,但是运行shell可使代码更短。

The code above was just a tiny snippet of my build file. Without exec-sync, I would either need to nest and nest and nest callbacks, or get a Deferred implementation to handle all the callbacks. Using a Deferred implementation would allow for more speed for the build process, but when the process takes only a few seconds, the tradeoff for top-down execution is worth it.

上面的代码只是我的构建文件的一小段。 没有exec-sync,我要么需要嵌套,嵌套和嵌套回调,要么需要获得Deferred实现来处理所有回调。 使用Deferred实现可以提高构建过程的速度,但是当过程仅花费几秒钟时,自上而下执行的权衡是值得的。

翻译自: https://davidwalsh.name/sync-exec

node .js 同步

你可能感兴趣的:(python,nodejs,vue,javascript,java)