简体   繁体   English

如何将函数的值传递给async.waterfall函数

[英]How to pass value of a function into a async.waterfall function

I have a problem with my code. 我的代码有问题。 I want to select from a Mysql table and considering the result, change it and update that table again. 我想从Mysql表中选择并考虑结果,对其进行更改并再次更新该表。 Problem is, the code do this asynchronous and it won't work properly (it do all SELECT first and all of UPDATE after). 问题是,代码会异步执行此操作,并且无法正常工作(首先执行所有SELECT,之后执行所有UPDATE)。 So I used async.js to get the job done, but still not working. 因此,我使用async.js完成了工作,但仍然无法正常工作。 Because I cant pass the Selected value out of caller callback. 因为我无法从调用者回调中传递出Selected值。 So it is not possible to pass it to next callback via async.waterfall. 因此,不可能通过async.waterfall将其传递给下一个回调。 Any ideas? 有任何想法吗?

async.waterfall([

    function (callback) {

        queryBuilder.selectAgent(connection, id, (agent) => {
            // Here is the PROBLEM! I can't pass agent out of this
        });

        // Here agent will be undefined
        callback(null, agent);
    },

    function (agent, callback) {

        if (agent) {
            // Some change on agent and pass it to next callback
        }

        callback(null, agent);
    },

    function (agent, callback) {

        queryBuilder.updateAgent(connection, id, agent);

        callback(null);
    },

],

function (err) {
    if (err) throw err;

})

您需要在updateAgent方法中进行更改,以检查先前的值并向其中添加新数据,或者您可以先检查任何特定项目的更新,然后收集它们以一次更新该字段。

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

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