Sha*_*rde 2 javascript promise bluebird
我正在尝试使用bluebird进行基本的承诺测试,但结果并不是我所期待的.我正在尝试将"我应该先登录吗?4000"消息首先登录到我的控制台,但第二个控制台消息总是首先显示.我做错了什么,在2000年消息之前我需要做些什么来获取"4000"消息?
function longRunningPositiveTest3() {
return new Promise(function(resolve) {
setTimeout(function () {
console.log("Should I be logged first? 4000");
resolve();
}, 4000);
});
}
function longRunningPositiveTest4() {
return new Promise(function(resolve) {
setTimeout(function () {
console.log("Should I be logged first? 2000");
resolve();
}, 2000);
});
}
Promise.resolve(longRunningPositiveTest3())
.then(longRunningPositiveTest4());
Run Code Online (Sandbox Code Playgroud)
谢谢
您正在调用两个立即启动超时的函数.你需要传递一个回调函数then!
使用
Promise.resolve(…) // some starting promise
.then(longRunningPositiveTest3)
.then(longRunningPositiveTest4);
Run Code Online (Sandbox Code Playgroud)
要么
longRunningPositiveTest3() // no need to wrap it in Promise.resolve() (but no harm either)
.then(longRunningPositiveTest4);
Run Code Online (Sandbox Code Playgroud)