Ajax承诺不起作用

I'm trying to use promise to return a comparison of current logged in user and a field from a list in SharePoint.

function compareCurrentUserWithListObject() {
   var userProp = this._userProfileProperties;
   var userName = userProp.get_userProfileProperties()['UserName'];

   return this._list.filter(function (element, index, array) {
    var promise = jQuery.ajax({
       url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
       type: "GET",
       headers: { "Accept": "application/json;odata=verbose" }
     });
     promise.done(function(data) {
        return (data.d.Email.indexOf(userName) > -1);
     });
   });
}   

function init() {
   var userArray = this.compareCurrentUserWithListObject();
   userArray.done(function(res) {
     if (res.length > 0) {
       //Do some stuff after compare...
     }
   });
}

I'm not sure I'm using the .done correct here. Can someone help me?

EDIT:

Working code:

function compareCurrentUserWithListObject() {
   var userProp = this._userProfileProperties;
   var userName = userProp.get_userProfileProperties()['UserName'];

   return this._list.filter(function (element, index, array) {
    var promise = jQuery.ajax({
       url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
       type: "GET",
       headers: { "Accept": "application/json;odata=verbose" }
     });
     promise.done(function(data) {
        return (data.d.Email.indexOf(userName) > -1);
     });
    return promise;
   });
}   

function init() {
   var userArray = this.compareCurrentUserWithListObject();
     if (userArray.length > 0) {
       //Do some stuff after compare...
     }
}

you need to return the promise

function compareCurrentUserWithListObject() {
   var userProp = this._userProfileProperties;
   var userName = userProp.get_userProfileProperties()['UserName'];

   return this._list.filter(function (element, index, array) {
    var promise = jQuery.ajax({
       url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
       type: "GET",
       headers: { "Accept": "application/json;odata=verbose" }
     });
     promise.done(function(data) {
        return (data.d.Email.indexOf(userName) > -1);
     });
     // return promise here
     return promise;
   });
}   

or this (which is cleaner IMO):

function compareCurrentUserWithListObject() {
   var userProp = this._userProfileProperties;
   var userName = userProp.get_userProfileProperties()['UserName'];


    return jQuery.ajax({
       url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
       type: "GET",
       headers: { "Accept": "application/json;odata=verbose" }
    });

}   


function init() {
   this.compareCurrentUserWithListObject()
   .done(function(data) {
     var res = data.d.Email.indexOf(userName) > -1;
     if (res.length > 0) {
       //Do some stuff after compare...
     }
   });

}

it looks like you want to modify the response before using it in init. There is a way to do that but I'd do it inside the .done callback when using it.

I didn't test this code so there might be mistakes. But the general answer is: you need to return the promise.

The idiomatic way to do this using promises is to use Promise.all(). (I'll use Q promises as an example, but Promise.all is built into the JS6 promise API and several other promise libraries):

function getUserInfo(listItem) {
    var promise = jQuery.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + 
             "/_api/web/GetUserById(" + listItem.user.get_lookupId() + ")",
        type: "GET",
        headers: { "Accept": "application/json;odata=verbose" }
    });
    return Q.Promise.when(promise);
}

function filterUsers(users) {
    var userProp = this._userProfileProperties;
    var userName = userProp.get_userProfileProperties()['UserName'];

    return users.filter(function (user) {
        return user.d.Email.indexOf(userName) > -1;
    });
}

function init() {
    Q.Promise.all(this._list.map(getUserInfo))
    .then(filterUsers.bind(this))
    .then(function (matchedUsers) {
       // matchedUsers is an array of all the users you want.
        // use it as needed
    });
}