【问题标题】:How to use promises in Angular and get rid of using timeouts [duplicate]如何在Angular中使用承诺并摆脱使用超时[重复]
【发布时间】:2018-10-19 13:01:31
【问题描述】:

我正在尝试等待服务器返回一些数据,然后再进行一次回调。这是我的代码

commonApp.factory('replyMessageFactory', ['$q', '$http', '$log', function ($q, $http, $log) {
var factoryObj = {};

factoryObj.send = function (obj) {
    var req = {
        method: 'POST',
        url: "/api/ThreadMessage",
        headers: {
            'Content-Type': 'application/json; charset=utf-8'
        },
        data: obj
    }

    return $http(req);
}
return factoryObj;
}
]);

我如下调用这个工厂方法

scope.sendReply = function () {
     updateModel();
     var replyModelValue = scope.replyModel;
     replyMessageFactory.send(scope.replyModel).then(function (data) {
             scope.clear();
             scope.updateThread(replyModelValue);
     });
}

问题是我没有从后端获取最新数据。如果我添加超时,那么它可以工作。会不会也是后端的原因?

我的后端api是这个

public async Task<HttpResponseMessage> Post(ThreadMessageModel messageData)
{

try
    {
        await _findMessageService.AddMessageReply(findmessage);
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
    catch (Exception exception)
    {
        Log.Log(EventSeverity.Error, new { Message = "Could not create message", exception });
        throw new Exception(string.Format("Kan ikke opprette melding. {0}", exception));
    }
}


public Task<int> AddMessageReply(FindMessage message)
{
try
    {
        client.Update<FindMessage>(message.Id).Field(x => x.Replies, message.Replies).Execute();
         return Task.FromResult(1);
    }
    catch (Exception exception)
    {
        log.Log(EventSeverity.Error, "Could not add message and feedback status", exception);
        throw;
    }
}

有什么想法吗?

【问题讨论】:

  • $http 返回一个承诺,所以using $q is not needed,这是一个反模式。你可以试试:return $http.post(url,obj).then((res) =&gt; {return res.data;})
  • 这样也行不通
  • 请告诉我们您是如何呼叫send 以及updateThread 是做什么的。您发布的第二个 sn-p 看起来不错。
  • 更新了问题
  • 您最多有 4 张重新打开投票,所以再多一张,它将重新打开。我怀疑我们有一个金票持有者在其中一个被骗者身上发现了类似的答案,但这不是标记为重复问题的正当理由。

标签: javascript c# angularjs angular-promise


【解决方案1】:

您不应该将 scope.updateThread 传递给工厂,而应该只返回 $http 承诺。

commonApp.factory('replyMessageFactory', ['$q', '$http', '$log', function ($q, $http, $log) {

    var factoryObj = {};
    factoryObj.send = function (obj) {

        var req = {
            method: 'POST',
            url: "/api/ThreadMessage",
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            data: obj
        };
        return $http(req);
    }    
    return factoryObj;
  }
]);

你会这样称呼它:

scope.sendReply = function () {
    replyMessageFactory.send(scope.replyModel)
        .then(function (result) {
            scope.updateThread(result);
        });
}

现在您的scope.updateThread 可以同步工作了。


附:关心scope.updateThread参数中的错字,应该是:

scope.updateThread = function (obj) {
    getMessages.getMessageById(obj, scope.updateThreadCallback);
}

【讨论】:

  • 不工作,有时它得到最新的,有时它没有
  • 你能告诉我们你的代码吗?一个工作的 sn-p 会很好
  • 我已经更新了代码
猜你喜欢
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多