【问题标题】:Converting async code to q/promise code in nodejs在 nodejs 中将异步代码转换为 q/promise 代码
【发布时间】:2015-04-15 18:21:45
【问题描述】:

我已经阅读了很多关于 promisejs 的不同文章,但似乎无法让它适用于我的代码。我有异步代码可以工作并且可以满足我的需要,但是它很长而且看起来不像 promise 那样干净。

这是我一直在研究的两个链接:http://jabberwocky.eu/2013/02/15/promises-in-javascript-with-q/https://spring.io/understanding/javascript-promises

mainCode.js

accountModel.findOne({username: body.username}, function(err, usernameFound) {
    console.log("here");
    if (err) {
        console.log(err);
    } else {
        console.log("here1");
        anotherClass.duplicateUsername(usernameFound, function(err, noerr) {
            if (err) {
                console.log("error");
                res.status(409).send("username");
            } else {
                console.log("here2");
                accountModel.findOne({email: body.email}, function(err, emailFound) {
                    if (err) {
                        console.log("error2");
                    } else {
                        console.log("here3");
                        console.log(emailFound);    
                    }
                });
            }
        });
    }
});

// anotherclass.duplicateUsername

anotherClass.prototype.duplicateUsername = function(usernameFound, callback) {
    if (usernameFound) {
        callback(usernameFound);
    } else {
        callback();
    }
}

当前的承诺代码(在 mainCode.js 中):

var promise = userModel.findOne({
    username: body.username
}).exec();

promise.then(function(usernameFound) {
    console.log("usernameFound")
    return userCheck.duplicateUsername(usernameFound);
}).then(function(usernameFound) {
        console.log("NOERR:" + usernameFound + ":NOERR");
        console.log("noerror");
        return;
    }, function(error) {
        console.log(err);
        console.log("error");
        res.sendStatus(409);
        return;
    });

当我运行我的 promise 代码时,它会转到 duplicateUsername,执行 callback(),但不会在 promise 代码中打印任何内容。

【问题讨论】:

  • 看起来你需要承诺化duplicateUsername。它还需要返回一个 Promise,而不是执行回调。
  • 但是链接中的示例都没有吗?另外,我该怎么做呢?
  • 您需要执行q.resolveq.reject,然后返回延迟的承诺,而不是执行回调。
  • 类似this?
  • @user1883614:你在找stackoverflow.com/q/22519784/1048572吗?

标签: node.js asynchronous promise q


【解决方案1】:

duplicationUsername 需要返回一个 Promise,否则 Promise 链将获取调用 callback 的返回值(即 undefined)。

这样的事情应该可以工作:

anotherClass.prototype.duplicateUsername = function(usernameFound) {
    var deferred = Q.defer();
    if (usernameFound) {
        deferred.resolve(usernameFound);
    } else {
        deferred.reject();
    }
    return deferred.promise;
}

【讨论】:

  • 我做到了,但似乎没有多大作用?如果 usernameFound 为空,那么它不应该调用 function(err) 但这就是它正在做的事情。如何处理承诺?
  • 我是否应该更改 mainClass.js 中的承诺代码以适应重复用户名的更改?
  • 差不多了。我需要添加: deferred.promise.nodeify(callback);修复它。
【解决方案2】:

所以我似乎需要先“承诺”我自己的功能,然后才能使用它们。

这是我使用 Q 的方法:

var Q = require('q');
anotherClass.prototype.duplicateUsername = function(username, callback) {
    return new Promise(function(resolve, reject) {
        var deferred = Q.defer();

        if (usernameFound) {
            deferred.reject("error);
        } else {
            deferred.resolve("no err: duplicate username");
        }

        deferred.promise.nodeify(callback);
        return deferred.promise;
    });
}

以下是使用 Bluebird 的方法:

userCheck.prototype.duplicateUsername = function(usernameFound) {
    return new Promise(function(resolve, reject) {
        if (usernameFound) {
            reject("error");
        } else {
            resolve();
        }
   });
}

然后在我的 mainClass 中,我只是通过调用方法然后 .then(//blah) 来使用它们

【讨论】:

    猜你喜欢
    • 2015-10-24
    • 2018-09-04
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2017-01-09
    • 2023-03-12
    相关资源
    最近更新 更多