阿贾克斯承诺不起作用

Ajax promise not working

本文关键字:不起作用 承诺 阿贾克斯      更新时间:2023-09-26

我正在尝试使用 promise 返回当前登录用户和 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...
     }
}

你需要返回承诺

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回调中执行此操作。

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

使用承诺执行此操作的惯用方法是使用 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
    });
}