承诺与承诺解决分开 [英] promise call separate from promise-resolution

查看:123
本文介绍了承诺与承诺解决分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对承诺不太熟悉。
我想隐藏promise-call中的promise-implementation。

I'm not so familiar with promises. I would like hide promise-implementation from promise-call.

示例:

function findFriends(req, res) {

    const promiseFriend  = MyFriendes.find({}).exec(); //call promise

    if(friends.length===0){
        logger.warn('No friendsavailible');
    }else if(friends === undefined){
        res.status(500).json({
            error: 'INTERNAL ERROR'
        });
    }else{
        res.status(200).json({
            friends: friends
        });
    }
} 

我会在同一档案中解决我的承诺但不会
在同一个函数中,我称之为这个承诺。

and I will resolve my promise within same file but not at same function, where I call this promise.

 promiseFriend  
        .then(function(friends){
            return friends;
        })
        .catch(function(err){
            logger.error({error:err});
        });

现在,我知道,promiseFriend未定义。
如何将promise-call与promise-resolution分开?

Now, I get, that "promiseFriend" is undefined. How can I separate promise-call from promise-resolution?

推荐答案

如果你想在一个承诺中定义一个承诺函数并在其他地方使用它然后首先需要从该函数返回promise,而这在您的代码中没有。然后你需要实际调用你也没做的那个函数。最后你需要在返回的值上使用然后回调,在这种情况下你也不会这样做。

If you want to define a promise in a function and use it somewhere else then first of all you need to return the promise from that function, which you're not doing in your code. Then you need to actually call that function which you are also not doing. And finally you need to use a then callback on the returned value, which you are not doing in this case as well.

在本地变量 promiseFriend 中保存承诺是没有意义的,该变量是作用于此函数的。也没有必要在然后回调中返回一个值: .then(function(friends){return friends;}) - 我不知道这里有什么尝试。

There is no point in saving the promise in a local variable promiseFriend that is scoped to this function. There is also no point to return a value in your then callback: .then(function (friends) { return friends; }) - I have no idea what have tried to do here.

我认为 findFriends 应该是是Express的路由处理程序。如果是这样,那么请确保在每种情况下都发送一个回复(你没有为 friends.length === 0 做)。此外,如果要在解析后执行操作,则需要向已保存的承诺添加然后处理程序。现在你甚至没有在你的函数中定义朋友。还要添加一个 catch 处理程序,并发送该案例的响应。

I suppose that findFriends is supposed to be a route handler for Express. If so then make sure that you send a response in every case (which you don't do for friends.length===0). Also, you need to actually add a then handler to the saved promise if you want to act when it's resolved. Right now you don't even have friends defined in your function. Also add a catch handlers and also send a response for that case.

然后你可以从你的回复中回复功能但不是如果它是路由处理程序,它没有意义。您可以从函数返回一个承诺:

Then you might return the promise from your function but not if it is a route handler, it doesn't make sense. You can return a promise from a function:

function x() {
  return MyFriendes.find({}).exec();
}

然后使用它:

x().then(friends => ...).catch(error => ...);

但是如果你不返回它就不能使用返回值,你不能使用未定义的变量好像它们已被定义,你实际上需要考虑返回的返回值是谁。

but you cannot use return values if you don't return it, you can't use undefined variables as if they were defined, and you actually need to consider who is your return value returned to.

我建议您了解Node实际上是如何工作的,因为它似乎你有复制并粘贴一些随机代码,将它们连接在一起并期望它能够实现您想要的而不会实际理解它。

I suggest that you learn how Node actually works because it seems that you have copied and pasted some random code, joined it together and expect that it does what you want without actually trying to understand it.

为了更好地理解异步性质在此处提供此执行订单的节点,请参阅以下答案:

To get a better understanding on the asynchronous nature of Node that is giving this execution order here, see those answers:

  • A detailed explanation on how to use callbacks and promises
  • Explanation on how to use promises in complex request handlers
  • An explanation of what a promise really is, on the example of AJAX requests
  • An explanation of callbacks, promises and how to access data returned asynchronously

在理解函数调用,返回值,回调以及本例承诺的概念之前,不要尝试编写Node程序。

Don't try to write Node programs before you understand the concept of function calls, return values, callbacks and in this case promises.

这篇关于承诺与承诺解决分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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