简体   繁体   English

ECMAScript 6承诺

[英]ECMAScript 6 promises

I'd like understand promises, but I have problem. 我想了解诺言,但是我有问题。

function commentFirst() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('first');
      resolve('value');
    }, 1500);
  });
}

commentFirst().then((val) => {
  setTimeout(function() {
    console.log(val + ' 1')                   
  }, 2000);

  return val;
}).then((val1) => console.log(val1 + ' 2'));

I want get output like this: 我想要这样的输出:

first
value 1
value 2

What am I doing wrong? 我究竟做错了什么?

In the second .then , you are doing a setTimeout without any promise, so it will execute and return immediately, even before the setTimeout executes. 在第二个.then ,您正在做一个setTimeout而没有任何承诺,因此它会立即执行并返回,甚至在setTimeout执行之前。 I've added a promise such that when the setTimeout executes, it will resolve the promise, and then continue executing the order you need. 我添加了一个保证,以便在执行setTimeout时,它将解决该保证,然后继续执行所需的命令。

function commentFirst() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('first');
      resolve("value");
    }, 1500);
  });
}

commentFirst()
  .then((val) => {
    return new Promise(resolve => {
      setTimeout(function() {
        console.log(val + ' 1')
        resolve(val);
      }, 2000);
    })
  })
  .then((val1) => console.log(val1 + ' 2'));

codepen demo Codepen演示

You can improve your code by creating multiple promise, to make your code more reusable. 您可以通过创建多个Promise来改进代码,以使您的代码更具可重用性。

So, you will be able to chain your promise . 因此,您将能够兑现您的承诺

But, you should see the Promise.all() method which allow you to returns a promise that resolves when all of the promises in the iterable argument have resolved. 但是,您应该看到Promise.all()方法,该方法允许您返回一个承诺,当可迭代参数中的所有promise已解决时,该promise将被解析。

You can decalre two function which will return some promise : 您可以将两个函数贴花,这将返回一些承诺:

function first(){
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve('first');
    }, 1000);
  });
}

function commentFirst(id){
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve('value ' + id);
    }, 1000);
  });
}

Then, you've got two options : 然后,您有两个选择:

  • Option 1 : Perform a chaining promise 选项1:履行连锁承诺
  • Option 2 : Use the Promise.all() method 选项2:使用Promise.all()方法

Option 1 : chaining 选项1:链接

first().then((data) => {
  //Log first
  console.log(data);
  //Return promise to print value 1
  return commentFirst(1);
}).then((data) => {
  //Log value 1
  console.log(data);
  //Return promise to print value 2
  return commentFirst(2);
}).then((data) => {
  //log value 2
  console.log(data);
});

Option 2 : Promise.all() 选项2:Promise.all()

Promise.all([first(), commentFirst(1), commentFirst(2)]).then((data) => {
  data.forEach((elm) => console.log(elm));
});

As you can see, option 2 is shorter than option 1 . 如您所见, 选项2选项1短。 In fact, the .all() method is used to handle multiple promise, and return a single promise with all the results. 实际上, .all()方法用于处理多个promise,并返回所有结果的单个promise。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM