本文介绍了按顺序运行JavaScript promises.一个接一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图兑现承诺(对每个承诺进行一些其他的API调用).根据两个诺言的结果,使用第三个函数来处理数据.同样,顺序运行的功能可能会发生变化.因此,我以一种动态的方式来建立承诺.

I am trying to run to promises (which make some rest API calls on each). Based on the result of the two promises, use the third function to process the data. Also, the functions running in sequence can be changing. So I made them in a dynamic way of constructing promise.

但是,诺言似乎是在没有前一个结局的情况下开始的.这是用于测试相同概念的简化模型.

However, the promises seem to be starting without the previous one ending. Here is a simplified model for testing the same concept.

var funcs = [
	() => {
    console.log('Task 1 Sart =', new Date());
  	sleeper(6000, 'Task 1  Resolved');
    console.log('Task 1  Return');
  },
  () => {
  console.log('Task 2  Sart=', new Date());
  	sleeper(5000, 'Task 2  Resolved');
    console.log('Task 2  Return');
  },
  () => {
  console.log('Task 3  Start=', new Date());
  console.log('Task 2  Return');
  }
];

function sleeper(m, msg) {
	return new Promise(function(resolve, reject) {
  	setTimeout(function() {
    resolve(console.log(msg, new Date()));
  	}, m);
	});
}

function runSequence(functionSequence) {
   return functionSequence.reduce((p, func) => {
       return p.then(func);
   }, Promise.resolve());
}

var v = runSequence(funcs);

结果看起来像这样VM128:51任务1 Sart = 2017年6月7日星期三13:54:34 GMT-0700(PDT)VM128:53任务1返回VM128:56任务2 Sart = 2017年6月7日星期三13:54:34 GMT-0700(PDT)VM128:58任务2返回VM128:61任务3开始= 2017年6月7日星期三13:54:34 GMT-0700(PDT)VM128:62任务2返回VM238:69任务2已解决2017年6月7日星期三13:54:39 GMT-0700(PDT)VM238:69任务1已解决2017年6月7日星期三13:54:40 GMT-0700(PDT)

The results look like thisVM128:51 Task 1 Sart = Wed Jun 07 2017 13:54:34 GMT-0700 (PDT)VM128:53 Task 1 ReturnVM128:56 Task 2 Sart= Wed Jun 07 2017 13:54:34 GMT-0700 (PDT)VM128:58 Task 2 ReturnVM128:61 Task 3 Start= Wed Jun 07 2017 13:54:34 GMT-0700 (PDT)VM128:62 Task 2 ReturnVM238:69 Task 2 Resolved Wed Jun 07 2017 13:54:39 GMT-0700 (PDT)VM238:69 Task 1 Resolved Wed Jun 07 2017 13:54:40 GMT-0700 (PDT)

我认为直到第一个任务结束,我才看到第二个任务开始.看起来它们都是按顺序开始的.是我遗漏的东西还是完全误解了诺言的工作方式.我试图实现的是先完成第一个,然后开始下一个

I would assume I don't see the second task start till the first one ended. Looked like they all started in sequence. Is anything I missed or totally misunderstand how the promises work. What I tried to achieve is to have the first one completed and then the following starts

推荐答案

基于注释.这是版本的作品.原始代码缺少简单的退货

Based on the comments. Here is the version works. A simple return is missing from original codes

var funcs = [
	() => {
    console.log('Task 1 Sart =', new Date());
  	return sleeper(7000, 'Task 1  Resolved');
  },
  () => {
  	console.log('Task 2  Sart=', new Date());
  	return sleeper(3000, 'Task 2  Resolved');
  },
  () => {
  console.log('Task 3  Start=', new Date());
  console.log('Task 3  Return');
  }
];

function sleeper(m, msg) {
	return new Promise(function(resolve, reject) {
  	setTimeout(function() {
    resolve(console.log(msg, new Date()));
  	}, m);
	});
}

function runSequence(functionSequence) {
   return functionSequence.reduce((p, func) => {
     return p.then(func);
   }, Promise.resolve());
}

runSequence(funcs);

这篇关于按顺序运行JavaScript promises.一个接一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:10