William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

NodeJS: Everyauth’s promise

NodeJS: Everyauth’s promise

When using Everyauth, I am confused about the concept promise. Here I found a helpful explanation for 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. The following function returns promise, but I never use this object anywhere.

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

From wiki, promise has the following definition:

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: