异步转换code到Q /承诺code中的NodeJS [英] Converting async code to q/promise code in nodejs

查看:141
本文介绍了异步转换code到Q /承诺code中的NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过promisejs了很多不同的文章阅读,但似乎无法得到它为我的code工作。我有异步code的作品和做什么,我需要的,但它很长,看起来不干净,因为它可以与承诺。

I've read through a lot of different articles on promisejs but can't seem to get it to work for my code. I have async code that works and does what I need but it's very long and doesn't look as clean as it could with promise.

下面是两个环节,我一直非常期待到:的 http://jabberwocky.eu/2013/02/15/promises-in-javascript-with-q/ 并的 https://spring.io/understanding/javascript-promises

Here's the two links I've been really looking into: http://jabberwocky.eu/2013/02/15/promises-in-javascript-with-q/ and https://spring.io/understanding/javascript-promises.

主要code.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.duplicateUsername

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

目前的承诺code(主code.js):

current promise code (in 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;
    });

当我跑我的诺言code,它进入duplicateUsername,做回调(),但是没有打印在承诺code什么。

When I run my promise code, it goes to duplicateUsername, does callback() but then doesn't print anything in the promise code.

推荐答案

所以好像我需要promisify我自己的功能之前,我可以使用它们。

So it seems like I needed to "promisify" my own functions before I could use them.

下面就是我带Q做到了:

Here's how I did it with 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;
    });
}

下面是如何与蓝鸟做到这一点:

Here's how to do it with Bluebird:

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

然后在我的mainClass,我只是用他们通过调用方法,然后。然后(//等等)

Then in my mainClass, I just used them by calling the methods and then .then(//blah)

这篇关于异步转换code到Q /承诺code中的NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆