node的promise链式调用

var bPromise = new Promise(function (resolve) {
    console.log('start');
    resolve(100);
});
bPromise.then(function (value) {
    return value * 2;
}).then(function (value) {
    return value * 2;
}).then(function (value) {
    console.log("2: " + value); // => 100 * 2 * 2
});


输出如下:
start
2: 400

上述代码看上去,promise里的函数只执行了一次(只有一个start输出),哪怕有多个then。
确实如此,原因是:
每个执行then的promise对象是一个新对象!由上个then返回了一个新的对象。

猜你喜欢

转载自xieye.iteye.com/blog/2399848