万事如意的承诺 [英] What are everyauth promises?

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

问题描述



我看到我需要退回承诺对象或用户,但是什么是allauth承诺? p>

解决方案

当您有一个执行身份验证的功能,但是异步执行时,这很有用。您不能直接从函数返回用户信息(因为您必须等待回调才能触发),因此您返回承诺。当异步请求完成时,这是一个特殊对象,充当最终将填充用户信息的占位符。



文档

 函数(session,accessToken,extra,oauthUser){
var promise = this.Promise();
asyncFindUser(function(err,user){
if(err)return promise.fail(err);
promise.fulfill(user);
});
退货承诺;
}

这意味着调用上下文可以继续做工作直到真的需要用户信息(和所有的同时,异步请求正在完成);如果用户信息尚不可用,则必须在稍后阶段等待。你可能会认为它是线程创建和加入的一个非常具体的例子。



Promise 是一种通用术语,涵盖了各种语言和上下文中的这种功能:


在计算机科学中,未来,承诺和延迟是指在某些并发编程语言中用于同步的结构。他们描述一个对象,它起初是不知道的结果的代理,通常是因为它的值的计算尚未完成。



I don't understand what everyauth promises are.

I see that I need to return a promise object or user, but what is an everyauth promise?

解决方案

It's useful when you have a function that performs authentication, but which does so asynchronously. You can't directly return user information from the function (because you have to wait for the callback to fire), so instead you return a promise. This is a special object that acts as a "placeholder" for what will eventually be filled with user information when the asynchronous request does complete.

Example from the documentation:

function (session, accessToken, extra, oauthUser) {
  var promise = this.Promise();
  asyncFindUser( function (err, user) {
    if (err) return promise.fail(err);
    promise.fulfill(user);
  });
  return promise;
}

It means that the calling context can carry on doing work right up until it really needs that user information (and all the while, in the meantime, the asynchronous request is completing); it would have to wait at that later stage if the user information wasn't yet available. You might think of it as a very specific case of thread creation and joining.

"Promise" is a generic term that covers this sort of functionality in all kinds of languages and contexts:

In computer science, future, promise, and delay refer to constructs used for synchronization in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially not known, usually because the computation of its value has not yet completed.

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

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