我已经开发JavaScript好几年了,我完全不理解关于承诺的大惊小怪。

似乎我所做的就是改变:

api(function(result){
    api2(function(result2){
        api3(function(result3){
             // do work
        });
    });
});

无论如何,我可以使用像async这样的库,比如:

api().then(function(result){
     api2().then(function(result2){
          api3().then(function(result3){
               // do work
          });
     });
});

代码更多,可读性更差。我在这里没有获得任何东西,它也没有突然神奇地“变平”。更不用说把事情变成承诺了。

那么,承诺有什么好大惊小怪的呢?


当前回答

Promises允许程序员编写比使用回调更简单、更易读的代码。


在一个程序中,有一系列的步骤需要执行。

function f() {
   step_a();
   step_b();
   step_c();
   ...
}

每一步之间通常都有信息传递。

function f() {
   const a = step_a(   );
   const b = step_b( a );
   const c = step_c( b );
   ...
}

其中一些步骤可能会花费(相对)较长的时间,所以有时您希望与其他事情并行执行这些步骤。一种方法是使用线程。另一个是异步编程。(这两种方法各有利弊,这里不再讨论。)这里,我们讨论的是异步编程。

在使用异步编程时,实现上述目标的简单方法是提供一个回调函数,在步骤完成时调用它。

// step_* calls the provided function with the returned value once complete.
function f() {
   step_a(
      function( a )
         step_b(
            function( b )
               step_c(
                  ...
               )
            },
         )
      },
   )
}

这很难读懂。承诺提供了一种简化代码的方法。

// step_* returns a promise.
function f() {
   step_a()
   .then( step_b )
   .then( step_c )
   ...
}

返回的对象被称为promise,因为它代表了函数的未来结果(即承诺的结果)(可以是值或异常)。

尽管承诺很有帮助,但使用承诺还是有点复杂。这就是async和await的用武之地。在声明为async的函数中,可以使用await来代替then。

// step_* returns a promise.
async function f()
   const a = await step_a(   );
   const b = await step_b( a );
   const c = await step_c( b );
   ...
}

不可否认,这比使用回调可读性强得多。

其他回答

除了已经确定的答案,用ES6箭头函数承诺从一个适度发光的小蓝矮星直接变成一个红巨星。它即将坍缩成超新星:

api().then(result => api2()).then(result2 => api3()).then(result3 => console.log(result3))

正如oligofren指出的,如果api调用之间没有参数,你根本不需要匿名包装器函数:

api().then(api2).then(api3).then(r3 => console.log(r3))

最后,如果你想达到超大质量黑洞的水平,你可以期待:

async function callApis() {
    let api1Result = await api();
    let api2Result = await api2(api1Result);
    let api3Result = await api3(api2Result);

    return api3Result;
}

Promises允许程序员编写比使用回调更简单、更易读的代码。


在一个程序中,有一系列的步骤需要执行。

function f() {
   step_a();
   step_b();
   step_c();
   ...
}

每一步之间通常都有信息传递。

function f() {
   const a = step_a(   );
   const b = step_b( a );
   const c = step_c( b );
   ...
}

其中一些步骤可能会花费(相对)较长的时间,所以有时您希望与其他事情并行执行这些步骤。一种方法是使用线程。另一个是异步编程。(这两种方法各有利弊,这里不再讨论。)这里,我们讨论的是异步编程。

在使用异步编程时,实现上述目标的简单方法是提供一个回调函数,在步骤完成时调用它。

// step_* calls the provided function with the returned value once complete.
function f() {
   step_a(
      function( a )
         step_b(
            function( b )
               step_c(
                  ...
               )
            },
         )
      },
   )
}

这很难读懂。承诺提供了一种简化代码的方法。

// step_* returns a promise.
function f() {
   step_a()
   .then( step_b )
   .then( step_c )
   ...
}

返回的对象被称为promise,因为它代表了函数的未来结果(即承诺的结果)(可以是值或异常)。

尽管承诺很有帮助,但使用承诺还是有点复杂。这就是async和await的用武之地。在声明为async的函数中,可以使用await来代替then。

// step_* returns a promise.
async function f()
   const a = await step_a(   );
   const b = await step_b( a );
   const c = await step_c( b );
   ...
}

不可否认,这比使用回调可读性强得多。

承诺不是回调,两者都是促进异步编程的编程习惯。使用协程或返回承诺的生成器使用异步/等待风格的编程可以被认为是第三种这样的习惯用法。这些习语在不同编程语言(包括Javascript)中的比较在这里:https://github.com/KjellSchubert/promise-future-task

除了上面这些精彩的答案,还可以再加2分:

1. 语义的差异:

应许可能在创世之时就已经解决了。这意味着他们保证条件,而不是事件。如果它们已被解析,则传递给它的已解析函数仍将被调用。

相反,回调处理事件。因此,如果您感兴趣的事件发生在注册回调之前,则不会调用回调。

2. 控制反转

回调涉及控制反转。当您向任何API注册回调函数时,Javascript运行时将存储回调函数,并在它准备运行时从事件循环中调用它。

有关解释请参考Javascript事件循环。

对于promise,控制驻留在调用程序中。如果存储promise对象,.then()方法可以在任何时候调用。

承诺不是回调。promise表示异步操作的未来结果。当然,按照你的方式来写,收效甚微。但是如果你按照它们应该被使用的方式来编写它们,你可以用一种类似于同步代码的方式来编写异步代码,并且更容易遵循:

api().then(function(result){
    return api2();
}).then(function(result2){
    return api3();
}).then(function(result3){
     // do work
});

当然,代码并没有减少多少,但是可读性大大提高了。

但这还没有结束。让我们来看看真正的好处:如果您想在任何步骤中检查任何错误,该怎么办?如果是回调就太糟糕了,但如果是承诺,那简直就是小菜一碟:

api().then(function(result){
    return api2();
}).then(function(result2){
    return api3();
}).then(function(result3){
     // do work
}).catch(function(error) {
     //handle any error that may occur before this point
});

几乎和尝试{…} catch块。

更好的是:

api().then(function(result){
    return api2();
}).then(function(result2){
    return api3();
}).then(function(result3){
     // do work
}).catch(function(error) {
     //handle any error that may occur before this point
}).then(function() {
     //do something whether there was an error or not
     //like hiding an spinner if you were performing an AJAX request.
});

更棒的是:如果这3个对api、api2、api3的调用可以同时运行(例如,如果它们是AJAX调用),但你需要等待这3个调用,那该怎么办?如果没有承诺,你就必须创造出某种反击。有了承诺,使用ES6符号,是另一块蛋糕,非常简洁:

Promise.all([api(), api2(), api3()]).then(function(result) {
    //do work. result is an array contains the values of the three fulfilled promises.
}).catch(function(error) {
    //handle the error. At least one of the promises rejected.
});

希望你现在能从新的角度看待承诺。