在一系列承诺完成或成功完成后继续

时间:2018-01-28 14:58:45

标签: javascript jquery promise es6-promise

我希望在所有承诺被解决或拒绝时继续,并在结尾处显示字符串“#####应该是最终调用####”。

<小时/> 的 TL; DR;

我忘了在地图功能中返回承诺。

我正在尝试使用这种方法: Wait until all ES6 promises complete, even rejected promises

以下代码有什么问题?

我希望.then的{​​{1}}处理程序最后会被调用,而不是所有其他人。

结果变量包含一个具有未定义值的数组。

Promise.all()
console.clear();

$(document).ready(function () {

    var url = "https://httpbin.org/get?";
    a = [];

    a.push(new Promise(function (resolve, reject) {
        $.get(url + Math.random()).then((res) => resolve()).fail(() => {
            reject();
        });
    }));
    a.push(new Promise(function (resolve, reject) {
        $.get(url + Math.random()).then((res) => resolve()).fail(() => {
            reject();
        });
    }));

    var mr = getPromise();

    function getPromise() {
        var promise = new Promise(function (resolve, reject) {
            window.setTimeout(function () {
                reject();

            }, 1000);
        });
        return promise;
    }

    a.push(mr);

    a.push(new Promise(function (resolve, reject) {
        $.get(url + Math.random()).then((res) => resolve()).fail(() => {
            reject();
        });
    }));

    a.push(new Promise(function (resolve, reject) {
        $.get(url + Math.random()).then((res) => resolve()).fail(() => {
            reject();
        });
    }));

    Promise.all(a.map((p) => {
            p.catch((e) => {
                console.log("failed promise catch");
            });
        }))
        .then((results) => {
            console.log("##### should be  final call  ####");
        })
        .catch((e) => {
            console.log("final catch block executed");
        });


}); // ready end

2 个答案:

答案 0 :(得分:2)

首先,避免Promise constructor antipattern!只需使用

function getPromise() {
  return new Promise(function(resolve, reject) {
    window.setTimeout(reject, 1000);
  });
}
const url = "https://httpbin.org/get?";
a = [
  Promise.resolve($.get(url+ Math.random())),
  Promise.resolve($.get(url+ Math.random())),
  getPromise(),
  Promise.resolve($.get(url+ Math.random())),
  Promise.resolve($.get(url+ Math.random()))
];

然后你的问题只是忘记return来自map回调的链接承诺(带有处理的拒绝),所以你在{{{{1}数组上调用Promise.all 1}}立即履行的。将其更改为:

undefined

答案 1 :(得分:1)

你忘了把诺言归还给地图。您承诺将立即解决未定义的数组。

<a href='{% url "club:team" %}'>team</a>
console.clear();

$(document).ready(function(){

function getPromise() {
  return new Promise((resolve, reject) => window.setTimeout(reject, 1000));
}

function getRandomUrl() {
  return new Promise((resolve, reject) => $.get(url+ Math.random()).done(resolve).fail(reject));
}

var url= "https://httpbin.org/get?";
a = [];

a.push(getRandomUrl());
a.push(getRandomUrl());
a.push(getPromise());
a.push(getRandomUrl());
a.push(getRandomUrl());

Promise.all(a.map(p => {
    return p.catch(console.log);   
  }))
  .then((results) => {
    console.log("##### should be  final call  ####");
  })
  .catch((e) => {
    console.log("final catch block executed");
  });


});// ready end