【问题标题】:Node run second function if successfully retried data from Angular post如果成功重试来自 Angular 帖子的数据,则节点运行第二个函数
【发布时间】:2017-06-27 15:54:06
【问题描述】:

我有一个 Angular 表单,我正在向 Node.js 发布数据。如果帖子重试成功,我想在成功时运行第二个函数。

到目前为止,我有三个函数在所有函数之后按顺序运行

Promise.all([firstFunction(), secondFunction()]) .then(thirdFunction);

如果成功接收到来自 Angular 的数据,我想运行第二个函数。

如果不成功,则将消息发送回 Angular 帖子

  res.status(500).send({ error: "email not retried:(" });

我的功能

var firstFunction = function () {
        return new Promise(function (resolve) {
            setTimeout(function () {
                app.post('/back-end/controller', function (req, res) {
                    console.log(req.body);
                    // res.status(500).send({ error: "email not retried:(" });
                    res.send('hello world');
                    // app.use('/login');
                    var login = req.body.LoginEmail;
                    // res.send(login);
                    resolve({
                        data_login_email: login
                    });
                });
                console.error("First done");
            }, 2000);
        });
    };

我的尝试

var firstFunction = function () {
        return new Promise(function (resolve) {
            setTimeout(function () {
                app.post('/back-end/controller', function (req, res) {
                    console.log(req.body);
                    var login = req.body.LoginEmail;

                    if (login.length !== 0) {
                        console.log("Success");
                        res.send('Success');
                        resolve({
                            data_login_email: login
                        });
                    } else {
                        console.log("Failed");
                        res.send('Failed');
                        // reject(reason);
                    }
                });
                console.error("First done");
            }, 2000);
        });
    };

【问题讨论】:

    标签: angularjs node.js express httprequest


    【解决方案1】:

    不会在第一个工作中添加第二个函数 类似的东西

    first().then(res => second ())
    

    目前您正在通过 promise.all 一起调用 first 和 second

    【讨论】:

    • so 而不是Promise.all([firstFunction(), secondFunction()]) .then(thirdFunction);first().then(res => second ()) ?
    • 是的。当第一个解决时,这将调用第二个
    • 好的,+1 但是只有当第一个函数为真时,我才能触发第二个?
    • 你应该通过developer.mozilla.org/en/docs/Web/JavaScript/Reference/…这是js的圣经而且他们有很好的文档
    • 抱歉昨晚早早离开了办公室,感谢您的帮助,祝您有美好的一天。
    猜你喜欢
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多