我有可变数量的异步函数来运行。对于两个函数,我可以这样写:
def calculation_of_total_cost
cost + (tip * cost)
end
如何在async.parallel中调用可变数量的函数?我尝试在async.parallel中添加for循环,但它不起作用。有什么建议吗?
答案 0 :(得分:1)
你可以像这样使用async.times函数
function(parallelCb) {
async.times(50, function (n, next) {
client.get('statuses/user_timeline', {screen_name:'rajeevnodetest'},
function(error, tweets, response){
next(err, tweets);
});
}, function (error, results) {
// do something with your results
parallelCb(null, {error: error, tweets: results});
}
}
所以如果你想在调用另一个方法之前运行n个异步函数你必须在其中使用async.series方法,因为根据async.parallel文档,它并行运行任务函数集合,无需等到上一个功能完成。
答案 1 :(得分:1)
我假设您将达到相同的API以便检索用户推文。
因此,您有一个包含所有高音扬声器句柄的数组:
KmCdmMakeMultiProjectDlg::KmCdmMakeMultiProjectDlg(CWnd* pParent)
: CDialog (KmCdmMakeMultiProjectDlg::IDD, pParent)
{
}
void KmCdmMakeMultiProjectDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_MULTIPART, lst_AddList);
DDX_Control(pDX, IDC_BTN_ADD_PROJECT, btn_AddList);
DDX_Control(pDX, IDC_BTN_ADDLIST_CSV, btn_AddList_CSV);
DDX_Control(pDX, IDC_BTN_DEL_PROJECT, btn_DelList);
DDX_Control(pDX, IDC_BTN_TARGET_SELECT, btn_ReferFolder);
DDX_Control(pDX, IDC_BTN_FILE_SELECT, btn_ReferCSV);
DDX_Control(pDX, IDC_BTN_EXECUTE, btn_Execute);
DDX_Control(pDX, IDC_BTN_EDIT_NAME, btn_EditName);
DDX_Control(pDX, IDC_BTN_EDIT_DESCRIPTION, btn_EditDescription);
DDX_Control(pDX, ID_CLOSE, btn_Close);
}
BEGIN_MESSAGE_MAP(KmCdmMakeMultiProjectDlg, CDialog)
ON_BN_CLICKED(IDC_BTN_ADD_PROJECT, &KmCdmMakeMultiProjectDlg::AddList)
ON_BN_CLICKED(IDC_BTN_ADDLIST_CSV, &KmCdmMakeMultiProjectDlg::AddListCSV)
ON_BN_CLICKED(IDC_BTN_DEL_PROJECT, &KmCdmMakeMultiProjectDlg::DelList)
ON_BN_CLICKED(IDC_BTN_TARGET_SELECT, &KmCdmMakeMultiProjectDlg::SelectPath)
ON_BN_CLICKED(IDC_BTN_FILE_SELECT, &KmCdmMakeMultiProjectDlg::SelectCSV)
ON_BN_CLICKED(IDC_BTN_EXECUTE, &KmCdmMakeMultiProjectDlg::MakeExecute)
ON_BN_CLICKED(IDC_BTN_EDIT_NAME, &KmCdmMakeMultiProjectDlg::EditName)
ON_BN_CLICKED(IDC_BTN_EDIT_DESCRIPTION, &KmCdmMakeMultiProjectDlg::EditDescription)
ON_BN_CLICKED(ID_CLOSE, &KmCdmMakeMultiProjectDlg::CloseDialog)
ON_WM_DROPFILES()
END_MESSAGE_MAP()
BOOL KmCdmMakeMultiProjectDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CDialog::DragAcceptFiles();
}
void KmCdmMakeMultiProjectDlg::OnDropFiles(HDROP hDropInfo)
{
CString csfile = "Why don't come this break point!";
CDialog::OnDropFiles(hDropInfo);
}
您可以定义一个函数来访问API,如下所示:
var screenNames = ['rajeevnodetest', 'rajeev_jayaswal', ..., 'brandnewhandle'];
然后你可以使用Asyn.map函数为你的tweeter句柄列表中的每个元素执行一次该函数,如:
//Custom function to find user status list
var findStatus = function(handle, callback) {
client.get('statuses/user_timeline', {screen_name: handle}, function(error, tweets, response){
callback(null, {error: error, tweets: tweets});
});
}
如果你需要为每个tweeter句柄调用多个API端点,那么你需要使用paralell并为每个端点创建一个条目。
答案 2 :(得分:0)
您应该查看EventEmitter
使用异步函数调用emit方法,并将要运行的所有函数挂钩到该事件名称