我知道可以链接承诺,如下例所示:
// we also have deferA, deferB and deferC as the corresponding defer objects
promiseB = promiseA.then(function(result) {
// do stuff to resolve B
});
promiseC = promiseB.then(function(result) {
// do stuff to resolve C
});
现在,如果我调用deferA.resolve(data)
this will resolve promiseA
,它的then
方法将运行,然后promiseB
被解析。最后 promiseBthen
将运行并解决promiseC
。平坦而简单(希望我做对了)。
但是,如果 Promise 与自身链接会发生什么?
如果我把上面的例子改成这样:
// we also have deferA and deferB as the corresponding defer objects
promiseA = promiseA.then(function(result) {
// do stuff to...?
});
promiseB = promiseA.then(function(result) {
// do stuff to resolve B
});
deferA.resolve(data);
现在会发生什么?执行顺序是什么?
更重要的是,这个呢:
// we also have deferA and deferB as the corresponding defer objects
promiseA = promiseA.then(function(result) {
// do stuff to...?
});
deferA.resolve(data);
promiseB = promiseA.then(function(result) {
// do stuff to resolve B
});
在这种情况下会发生什么?