【问题标题】:Resolve a chain of promises with timeouts. Promise.all用超时解决一系列承诺。承诺.all
【发布时间】:2018-03-18 12:00:08
【问题描述】:

我有这个。

const getPrice = function(database){
    return new Promise(function (resolve, reject){
        var promises =[];
        for(var i in database){
            promises.push(Update.requestClassifieds(database[i]))
        }
    Promise.all(promises)
        .then(function(todos){
            return resolve(todos);
        })
})}

Update.prototype.requestClassifieds = function(item){
    var ithis = this;
    return new Promise((resolve, reject) => {
       var input = {};
       request({
           url: '',
           method: "GET",
           json: true,  
           body: input
  }, function (error, response, body){
    if (error){
        return resolve(item);
    }
    else if(body){
        return resolve(item);
    }
  }
});
});
}

我需要为数据库中的每个项目请求数据。因此,我为此创建了一系列承诺。

我正在请求具有 5 秒冷却时间的 API 的数据。所以,我需要等待 5 秒,直到解决 Promise.all(promises) 中的下一个承诺。

如何在 Promise.all 中的每个 Promise 之间设置Timeout?

【问题讨论】:

  • 这是个坏主意,一个问题,为什么你需要在承诺之间倒计时 5 秒?有关数据库访问的任何信息?
  • @Kalamarico Api 我使用的冷却时间为 5 秒。

标签: javascript node.js request timeout


【解决方案1】:

您需要在循环中创建Promise

const getPrice = function (database) {
    var promisesChain = Promise.resolve(); // Promise chain
    var todos = []; // results to be stored
    for (var i in database) {
        promisesChain = promisesChain.then(((i) => () => { // create closure to grab database[i]
            return Update.requestClassifieds(database[i])
        })(i)).then((res) => {
            return new Promise((resolve, reject) => {
                setTimeout(() => { // store result of requestClassifieds and resolve after 5sec
                    todos.push(res);
                    resolve();
                }, 500);
            });
        })
    }
    return promisesChain.then(() => {return todos});
};

运行示例

const getPrice = function (database) {
	var promisesChain = Promise.resolve();
	var todos = [];
	for (var i in database) {
		promisesChain = promisesChain.then(((i) => () => {
			return Update.requestClassifieds(database[i])
		})(i)).then((res) => {
			return new Promise((resolve, reject) => {
				setTimeout(() => {
					todos.push(res);
					resolve();
          console.log('resolve', res);
				}, 1000);
			});
		})
	}
	return promisesChain.then(() => {return todos});
};
const Update = {};
Update.requestClassifieds = function (i) {
	console.log('got', i);
	return new Promise((resolve, reject) => {
		resolve(i)
	});
}
getPrice({
	a: 'A',
	b : 'B',
	c : 'C'
}).then(console.log);

【讨论】:

  • 我认为这是最好的解决方案。一个问题:如果我希望 getPrice 将结果作为数组返回,因为我想使用它们。只是我需要在“promisesChain.then(function () { console.log(todos); // display results })”中这样做
  • promisesChain.then(function () { console.log(todos); // display results }) 更改为promisesChain.then(() => {return todos});,因此getPrice 将返回Promise 以结果数组解析
猜你喜欢
  • 2016-09-02
  • 2015-04-09
  • 1970-01-01
  • 2019-09-29
  • 2015-11-17
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
相关资源
最近更新 更多