Javascript 为什么承诺在同一时间执行而不有序? 功能页(){ 返回新承诺((解决、拒绝)=>{ setTimeout(函数(){ fo=$(“#root>div>div>div>main>div>div:n子(3)>div>div:n子(2)>div>div:n子(2)>div:n子(2)>div>div:n子(2)>div>div:n子(2)>div>ul>li:n最后一个子(1)” fo.click() console.log('翻页') 解决(); }, 200) }) } 函数l(){ for(设i=0,p=Promise.resolve();i{ 返回新承诺(解决=> setTimeout(函数(){ console.log('wo') $(“#root>div>div>main>div>div:n子(3)>div>div:n子(2)>div:n子(2)>div:n子(2)>div>div:n子(2)>div>div:n子(1)>div:n子(1)>table>tbody>tr>td:n子(7)>div>div>div:n子(2)>a>span() 解决(); }, 200) ) }) .然后(()=>{ 返回新承诺(解决=> setTimeout(函数(){ console.log('wocao') $('body>div:n最后一个子(1)>div>div>div:n子(3)>div:n子(2)>button>span')。单击() 解决(); }, 200) ) } ) } } Promise.resolve().then(l).then(第页)
为什么程序没有按照承诺的顺序运行?我如何解决这个问题?我已经尝试了很多次,但它们仍然一起执行,但没有按照承诺的顺序运行。有人可以教我吗?非常感谢。您需要在超时内解决问题Javascript 为什么承诺在同一时间执行而不有序? 功能页(){ 返回新承诺((解决、拒绝)=>{ setTimeout(函数(){ fo=$(“#root>div>div>div>main>div>div:n子(3)>div>div:n子(2)>div>div:n子(2)>div:n子(2)>div>div:n子(2)>div>div:n子(2)>div>ul>li:n最后一个子(1)” fo.click() console.log('翻页') 解决(); }, 200) }) } 函数l(){ for(设i=0,p=Promise.resolve();i{ 返回新承诺(解决=> setTimeout(函数(){ console.log('wo') $(“#root>div>div>main>div>div:n子(3)>div>div:n子(2)>div:n子(2)>div:n子(2)>div>div:n子(2)>div>div:n子(1)>div:n子(1)>table>tbody>tr>td:n子(7)>div>div>div:n子(2)>a>span() 解决(); }, 200) ) }) .然后(()=>{ 返回新承诺(解决=> setTimeout(函数(){ console.log('wocao') $('body>div:n最后一个子(1)>div>div>div:n子(3)>div:n子(2)>button>span')。单击() 解决(); }, 200) ) } ) } } Promise.resolve().then(l).then(第页),javascript,Javascript,为什么程序没有按照承诺的顺序运行?我如何解决这个问题?我已经尝试了很多次,但它们仍然一起执行,但没有按照承诺的顺序运行。有人可以教我吗?非常感谢。您需要在超时内解决问题 function page() { return new Promise((resolve, reject) => { setTimeout(function() { fo = $("#root>div>div>main>div>div:nth-
function page() {
return new Promise((resolve, reject) => {
setTimeout(function() {
fo = $("#root>div>div>main>div>div:nth-child(3)>div>div:nth-child(2)>div>div>div:nth-child(2)>div:nth-child(2)>div>div:nth-child(2)>div>div:nth-child(2)>div>ul>li:nth-last-child(1)")
fo.click()
console.log('翻页')
resolve();
}, 200)
})
}
function l() {
for (let i = 0, p = Promise.resolve(); i < 10; i++) {
p = p.then(() => {
return new Promise(resolve =>
setTimeout(function() {
console.log('wo')
$('#root>div>div>main>div>div:nth-child(3)>div>div:nth-child(2)>div>div>div:nth-child(2)>div:nth-child(2)>div>div:nth-child(2)>div>div:nth-child(1)>div:nth-child(1)>table>tbody>tr>td:nth-child(7)>div>div:nth-child(2)>a>span').eq(i).click()
resolve();
}, 200)
)
})
.then(() => {
return new Promise(resolve =>
setTimeout(function() {
console.log('wocao')
$('body>div:nth-last-child(1)>div>div>div:nth-child(3)>div:nth-child(2)>button>span').click()
resolve();
}, 200)
)
}
)
}
}
Promise.resolve().then(l).then(page)
但在您的情况下,您没有为承诺定义参数,因此它们是默认值。您的
函数l
是一个单位函数(即不返回任何内容),因此不可等待,因为它不返回承诺(无论是显式的还是通过标记为异步的)
你应该:
new Promise(function(resolve, reject) {
// the function is executed automatically when the promise is constructed
// after 1 second signal that the job is done with the result "done"
setTimeout(() => {console.log("done");resolve()}, 1000);
}).then(function (res2, rej2) {
setTimeout(() => {console.log("done2")}, 1000);
})
函数l(){
让p=Promise.resolve()//这需要存在于循环范围之外
for(设i=0;i<10;i++){
p=p.then(/*…*/)
}
return p;//函数l
不返回承诺,因此无法等待(有效地立即解决,触发页面函数)通过正确设置代码格式,可以更轻松地推断发生了什么。例如,使用您还应该考虑使用函数名称,它看起来不象数字,看起来像是忘记了<代码>在代码>函数> L/CODE结束时返回P<代码>
function l() {
let p = Promise.resolve() // this needs to exist out of the loop scope
for (let i = 0; i < 10; i++) {
p = p.then( /* ... */ )
}
return p; // <-- so it can be returned here
}