What are everyauth promises?

混江龙づ霸主 提交于 2020-01-01 04:09:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8761230/what-are-everyauth-promises

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!