我已经开发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
          });
     });
});

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

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


当前回答

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

其他回答

除了其他答案,ES2015语法与承诺无缝融合,减少了更多的样板代码:

// Sequentially:
api1()
  .then(r1 => api2(r1))
  .then(r2 => api3(r2))
  .then(r3 => {
      // Done
  });

// Parallel:
Promise.all([
    api1(),
    api2(),
    api3()
]).then(([r1, r2, r3]) => {
    // Done
});

承诺不是回调。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.
});

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

没有承诺只是回调的包装

例子 你可以在node js中使用javascript原生承诺

my cloud 9 code link : https://ide.c9.io/adx2803/native-promises-in-node

/**
* Created by dixit-lab on 20/6/16.
*/

var express = require('express');
var request = require('request');   //Simplified HTTP request client.


var app = express();

function promisify(url) {
    return new Promise(function (resolve, reject) {
    request.get(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        resolve(body);
    }
    else {
        reject(error);
    }
    })
    });
}

//get all the albums of a user who have posted post 100
app.get('/listAlbums', function (req, res) {
//get the post with post id 100
promisify('http://jsonplaceholder.typicode.com/posts/100').then(function (result) {
var obj = JSON.parse(result);
return promisify('http://jsonplaceholder.typicode.com/users/' + obj.userId + '/albums')
})
.catch(function (e) {
    console.log(e);
})
.then(function (result) {
    res.end(result);
}
)

})


var server = app.listen(8081, function () {

var host = server.address().address
var port = server.address().port

console.log("Example app listening at http://%s:%s", host, port)

})


//run webservice on browser : http://localhost:8081/listAlbums

除了已经确定的答案,用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;
}

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

1. 语义的差异:

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

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

2. 控制反转

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

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

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