Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What are everyauth promises?

What are everyauth promises?

Problem

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?

Problem courtesy of: rizidoro

Solution

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.

Solution courtesy of: Lightness Races in Orbit

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

What are everyauth promises?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×