在 NodeJS 中,如何从虚函数中退出父函数
In NodeJS how do I exit a parent function from a virtual function
我有类似下面的内容
var async = require(async)
function start () {
async.series(
[
function (callback) {
// do something
callback(null, "Done doing something")
return // EXIT start() NOT JUST THIS VIRTUAL FUNCTION
},
function (callback) {
// do something else if the first thing succeeded
callback(null, "Done doing something else")
}
],
function (err,result) {
if (err) {
console.log(err)
}
}
)
console.log("All done!")
}
// Start doing stuff
start()
是否可以根据某些条件从我放置注释的包含的虚函数中退出函数 start() "EXIT start() NOT JUST THIS VIRTUAL FUNCTION"
在 async
文档中(此处:https://github.com/caolan/async#seriestasks-callback)
async.series
Run the functions in the tasks collection in series, each one running
once the previous function has completed. If any functions in the
series pass an error to its callback, no more functions are run
, and
callback is immediately called with the value of the error. Otherwise,
callback receives an array of results when tasks have completed.
所以,而不是调用:
return // EXIT start() NOT JUST THIS VIRTUAL FUNCTION
您可以拨打:
callback("stop", null)
总之是的,你可以提前退出系列任务。 async
使用来自 RisingStack 的 error-first callbacks, which are a fundamental async callback structure in Node.js. An additional resource for learning about Async development using Node.js see this great blog post。
来自 async
的回调得到 returned 到最终回调,如果提供,则在 async.series
完成时调用,此回调是可选的。如果系列 return 中的任务在第一个参数(错误参数)中的值不是 null
的回调,则 series
将中断并 return 到最后回调处理程序。
async.series(
[
function (callback) {
// If some condition is met, exit series with an error
if (someCondition)
return callback('exit start', null);
return callback(null, 'Done doing something');
},
function (callback) {
if (someCondition)
return callback('error', null);
// do something else if the first thing succeeded
return callback(null, "Done doing something else")
}
],
function (err,result) {
// Handle error returned from a series callback if one occurred
if (err) {
console.log(err)
}
}
)
我有类似下面的内容
var async = require(async)
function start () {
async.series(
[
function (callback) {
// do something
callback(null, "Done doing something")
return // EXIT start() NOT JUST THIS VIRTUAL FUNCTION
},
function (callback) {
// do something else if the first thing succeeded
callback(null, "Done doing something else")
}
],
function (err,result) {
if (err) {
console.log(err)
}
}
)
console.log("All done!")
}
// Start doing stuff
start()
是否可以根据某些条件从我放置注释的包含的虚函数中退出函数 start() "EXIT start() NOT JUST THIS VIRTUAL FUNCTION"
在 async
文档中(此处:https://github.com/caolan/async#seriestasks-callback)
async.series Run the functions in the tasks collection in series, each one running once the previous function has completed.
If any functions in the series pass an error to its callback, no more functions are run
, and callback is immediately called with the value of the error. Otherwise, callback receives an array of results when tasks have completed.
所以,而不是调用:
return // EXIT start() NOT JUST THIS VIRTUAL FUNCTION
您可以拨打:
callback("stop", null)
总之是的,你可以提前退出系列任务。 async
使用来自 RisingStack 的 error-first callbacks, which are a fundamental async callback structure in Node.js. An additional resource for learning about Async development using Node.js see this great blog post。
来自 async
的回调得到 returned 到最终回调,如果提供,则在 async.series
完成时调用,此回调是可选的。如果系列 return 中的任务在第一个参数(错误参数)中的值不是 null
的回调,则 series
将中断并 return 到最后回调处理程序。
async.series(
[
function (callback) {
// If some condition is met, exit series with an error
if (someCondition)
return callback('exit start', null);
return callback(null, 'Done doing something');
},
function (callback) {
if (someCondition)
return callback('error', null);
// do something else if the first thing succeeded
return callback(null, "Done doing something else")
}
],
function (err,result) {
// Handle error returned from a series callback if one occurred
if (err) {
console.log(err)
}
}
)