承诺调用与承诺解析分开 [英] promise call separate from promise-resolution

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

问题描述

我对 Promise 不太熟悉.我想从 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"是未定义的.如何将承诺调用与承诺解析分开?

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

推荐答案

如果你想在一个函数中定义一个 promise 并在其他地方使用它,那么首先你需要从那个函数返回 promise,你是没有在你的代码中做.然后你需要实际调用那个你也没有做的函数.最后,您需要对返回的值使用 then 回调,在这种情况下您也没有这样做.

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.

将promise 保存在局部变量promiseFriend 中是没有意义的,该变量的作用域是这个函数.在您的 then 回调中返回一个值也没有意义:.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,您不会这样做).此外,如果您想在解决时采取行动,您需要实际向保存的承诺添加一个 then 处理程序.现在你甚至没有在你的函数中定义 friends .还要添加一个 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.

为了更好地理解在这里给出此执行顺序的 Node 的异步性质,请参阅以下答案:

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

在理解函数调用、返回值、回调以及在本例中的承诺的概念之前,不要尝试编写 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天全站免登陆