用于自定义的 Nodejs Promise api

Nodejs Promise for custom api

我是 nodejs 的新手,正在使用 promise 实际上这是我使用 nodejs 的第一个真正的应用程序。

所以我看了一整天,有点迷茫

所以这是我的模块:

function User() {
    var self   = this;
    self.users = {};

    self.start = function (user, botId) {
        return new Promise(function () {
           return get(user).then(function (data) {
                debug(data);
                if (data.botId.indexOf(botId) === false) {
                    return Repo.UserBotModel.addUser(user.id, botId).then(function () {
                        data.botId.push(botId);
                        return data;
                    });
                } else
                    return data;
            });
        });
    };

    self.getDisplayName = function (user) {
        if (user.real_name)
            return user.real_name;
        if (user.last_name)
            return user.firstname + ' ' + user.last_name;
        return user.first_name;
    };
    /**
     * check if user exist in our database/memory cache and return it,
     * otherwise insert in the database and cache it in memory and the return it
     * @param user
     */
    function get(user) {

        return new Promise(function () {

            //check if user is loaded in our memory cache
            if (self.users.hasOwnProperty(user.id))
                return self.users[user.id];
            else {
                //get from database if exist
                return Repo.UserModel.get(user.id).then(function (rows) {
                    if (rows && rows.length) {
                        //user exist cache it and resolve
                        var data = rows[0];
                        if (data.botId && data.botId.length)
                            data.botId = data.botId.split(',');
                        else
                            data.botId = [];

                        self.users[user.id] = data;
                        //------------------------------ code execution reaches here
                        return data;
                    }
                    else {
                        //user dose not exist lets insert it
                        return Repo.UserModel.insert(user).then(function (result) {
                            return get(user);
                        });
                    }
                });
            }
        });
    }
}

我调用 start 方法 调用私有 get 方法 调用到达 return data;(标有注释)但是 then 函数没有在start 方法 ???

所以我做错了什么?

更新:抱歉,我忘了说我使用的是 bluebird 而不是 native promise 是否有区别?

- you have to call resolve (expected to happen asynchronously). You're not supposed to use the Promise constructor 完全在这里。你可以省略它,它应该可以工作。

  • 您的 Repo.UserModel 中的方法已经 return 承诺,因此您不必使用 new Promise 创建新的方法。
  • 您可以使用 then 读取这些承诺中的值。
  • then 还提供了一种转换 promise 的方法。如果您 return 传递给 then 的函数中的一个值,then 将 return 一个 new 承诺包装您 return编辑。如果这个值是一个承诺,它将被等待。
  • 要将值转换为承诺,您可以使用 Promise.resolve

了解这一点,您可以像这样简化 get

function get(user) {
    if (...) {
        return Promise.resolve(...)
    } else {
        return Repo.UserModel.get(...).then(function(rows) {
            ...
            return ...
        })
    }
}

此版本的 get 将始终 return 您可以像这样使用的承诺:

get(...).then(function(resultOfGet) {
    // process resultOfGet
})