Ajax 承诺无效

Ajax promise not working

我正在尝试使用 promise return 比较当前登录的用户和 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...
     }
   });
}

我不确定我在这里使用的 .done 是否正确。有人能帮我吗?

编辑:

工作代码:

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...
     }
}

你需要return承诺

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;
   });
}   

或这个(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...
     }
   });

}

您似乎想在 init 中使用之前修改响应。有一种方法可以做到这一点,但我会在使用它时在 .done 回调中进行。

我没有测试此代码,因此可能存在错误。但一般的回答是:你需要 return 承诺。

使用 promise 执行此操作的惯用方法是使用 Promise.all()。 (我将使用 Q promise 作为示例,但是 Promise.all 内置于 JS6 promise API 和其他几个 promise 库中:

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
    });
}