承诺:如何传递数据?

时间:2017-04-06 10:07:39

标签: javascript promise

我试图理解承诺是如何运作的。我认为它就像一个“链式回调”...或多或少......但是......可以提取你得到的外部值/数组/对象的值吗?

<div id="text"></div>

如何将数据从第一个Promise1() .then(return x) .then() .then() .then(function(x){ console.log(x); }); 传递到第四个.then()

任何帮助我理解承诺的努力都将受到赞赏。

3 个答案:

答案 0 :(得分:2)

您可以这样做,但您需要将/*Monster L*/ .monster_L img { position: absolute; bottom: 296px; left: 596px; z-index: 50; opacity: 1; width: 70px; } /*Been1*/ .B_1 { position: absolute; bottom: 293px; left: 597px; z-index: 40; opacity: 1; width: 8px; height: 50px; background-color: #297f40; border-radius: 0 0 15px 15px; animation-name: animation_B_1; animation-delay: 0s; animation-duration: 2s; animation-iteration-count: infinite; animation-timing-function: ease-in-out; } /*Been2*/ .B_2 { position: absolute; bottom: 286px; left: 605px; z-index: 40; opacity: 1; width: 8px; height: 50px; background-color: #297f40; border-radius: 0 0 15px 15px; animation-name: animation_B_2; animation-delay: 0s; animation-duration: 2s; animation-iteration-count: infinite; animation-timing-function: ease-in-out; } /*Been3*/ .B_3 { left: 613px; } /*Been4*/ .B_4 { left: 621px; } @keyframes animation_B_1 { 0%{ bottom: 293px; } 50% { bottom: 286px; } 100%{ bottom: 293px; } } @keyframes animation_B_2 { 0%{ bottom: 286px; } 50% { bottom: 293px; } 100%{ bottom: 286px; } } 值从一个x传递到另一个{

then

答案 1 :(得分:1)

Promise.prototype.then()方法返回Promise。它最多需要两个参数:Promise的成功和失败案例的回调函数。

因此,您可以使用多个then,因为您需要返回x并使用返回x的内容...等等

代码示例:

let array = [1, 2, 3, 4, 5, 6],
    Promise1 = x => {
      return new Promise((resolve, reject) => {
        // Your logic...
        setTimeout(() => 
          x <= 4 
            ? resolve('Success!') 
            : reject('Rejected!')
          , 600 * x);
      });
    },
    logMessage = (x, msg) => console.log(`For item ${x} result: ${msg}`);

array.forEach(x => Promise1(x)
  .then(x => x)
  .then(x => x)
  .then(x => x)
  .then(x => x)
  .then(message => logMessage(x, message))
  .catch(reason => logMessage(x, reason))
);

请注意,当thenPromise.prototype.catch()方法返回promises时,它们可以被链接 - 一个名为composition的操作。

答案 2 :(得分:-1)

1

doSomething().then(function () {
  return doSomethingElse();
}).then(finalHandler);

    doSomething
    |-----------------|
                      doSomethingElse(undefined)
                      |------------------|
                                         finalHandler(resultOfDoSomethingElse)
                                         |------------------|

2

doSomething().then(function () {
  doSomethingElse();
}).then(finalHandler);

doSomething
|-----------------|
                  doSomethingElse(undefined)
                  |------------------|
                  finalHandler(undefined)
                  |------------------|

3

doSomething().then(doSomethingElse())
  .then(finalHandler);

doSomething
|-----------------|
doSomethingElse(undefined)
|---------------------------------|
                  finalHandler(resultOfDoSomething)
                  |------------------|

4

doSomething().then(doSomethingElse)
  .then(finalHandler);

doSomething
|-----------------|
                  doSomethingElse(resultOfDoSomething)
                  |------------------|
                                     finalHandler(resultOfDoSomethingElse)
                                     |------------------|

https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html