I think Promise.resolve
and new Promise(resolve)
是可以互换的。
考虑一下:
A.
new RSVP.Promise(function (resolve, reject) {
resolve();
}).then(function () {
return new RSVP.Promise(function (resolve) {
resolve("HI")
});
}).then(function (result) {
console.log(result);
});
B.
new RSVP.Promise(function (resolve, reject) {
resolve();
}).then(function () {
return RSVP.resolve("HI");
}).then(function (result) {
console.log(result);
});
正如我所料,两者都打印“HI”。
所以我想如果我不需要“拒绝”任何东西。我可以写RSVP.resolve();
为了简单起见。
但考虑这个例子:
new RSVP.Promise(function (resolve, reject) {
resolve();
}).then(function () {
return new RSVP.Promise(function (resolve, reject) {
setTimeout(function () {
resolve("HI")
}, 3000);
});
}).then(function (result) {
console.log(result);
});
我该如何使用RSVP.resolve();
取代?我尝试过例如:
new RSVP.Promise(function (resolve, reject) {
resolve();
}).then(function () {
return setTimeout(function () {
return new RSVP.resolve("HI");
}, 3000);
}).then(function (result) {
console.log(result);
});
这会打印其他内容而不是“HI”。那么是否可以使用 RSVP.resolve();这里?这两个可以互换吗?