NodeJS: Everyauth's promise
Blogs20122012-06-13
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.
