javascript - 在 Angular.js 中对 $http 请求的结果进行排序

标签 javascript angularjs promise angular-promise

我有 3 个通过 $http 进行的网络请求,该请求位于函数中 (function1(), function2(), function3()) 。我想自定义所有这些请求的执行顺序。

object.function1().then(function() {
//result of petition $http of function1();
});

object.function2().then(function() {
//result of petition $http of function1();
});

object.function3().then(function() {
//result of petition $http of function2();
});

他们都尝试同时运行。有些请求比其他请求花费更长的时间,因为它们获取更多 JSON 对象。我想先运行:

function1();  //first
function2();  //second
function3();  //three

最佳答案

您需要正确使用.then()方法:

object.function1().then(function(result) {
    //result of petition $http of function1();

    return object.function2()
}).then(function (result) {
    //result of petition $http of function2();

    return object.function3();
}).then(function (result) {
    //result of petition $http of function3();
});

关于javascript - 在 Angular.js 中对 $http 请求的结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37582786/

相关文章:

css - 如何解决 Bootstrap 类名冲突

javascript - 异步函数是否仅用于 IO 阻塞操作?

javascript - 使用 getElementById 验证复选框数组

javascript - 通过axios上传文件到WebApi

javascript - php 文件上的安全 angularjs 代码 ajax 请求

javascript - 如何在 Angular 指令中调用我自己的函数?

javascript - Angular ui 路由器不起作用

javascript - Promise 链/通用代码约定/风格

node.js - Bluebird :WAITING一个 promise 的解决

javascript - 使用 XHR 和脚本 src 的区别