承诺的有条件解决

时间:2017-05-10 18:43:38

标签: javascript asynchronous promise

如果对象包含'four',在所有其他情况下为false

,我如何从promise中获得真正的决心

P.S。对不起我的英文

let asyncForEach = function(object, callback) {
	let p = Promise.resolve();
  for (const item of object) {
  	p = p.then(() => item).then(callback);
  }
  return p;
}

let asyncRequest = value => new Promise(resolve => 
	setTimeout(resolve(value), 150));

let object = ['one', 'two', 'three', 'four', 'five'];

let result = asyncForEach(object, asyncRequest);

result.then(resolve => document.getElementById('result').innerHTML = resolve)
<div id="result"></div>

0 个答案:

没有答案