Javascript 如果Promise haven';还没定下来吗?
当我们使用chain链接一个承诺的时,承诺由然后返回,那么在所有链完成之前,不属于然后链的代码如何运行?代码如何跳过然后调用?在运行“console.log”代码行之前,是否应该执行所有then调用 代码如下:Javascript 如果Promise haven';还没定下来吗?,javascript,asynchronous,Javascript,Asynchronous,当我们使用chain链接一个承诺的时,承诺由然后返回,那么在所有链完成之前,不属于然后链的代码如何运行?代码如何跳过然后调用?在运行“console.log”代码行之前,是否应该执行所有then调用 代码如下: Promise.resolve('333') .then((res)=>{console.log(res);return new Promise((resolve,reject)=> {setTimeout(function(){console.log('Timeou
Promise.resolve('333')
.then((res)=>{console.log(res);return new Promise((resolve,reject)=> {setTimeout(function(){console.log('Timeout');resolve(1);},10000);console.log('Something');})})
.then((res)=>{console.log(res);return new Promise((resolve,reject)=> {console.log('Something');resolve(2);})})
.then((res)=>{console.log(res);return new Promise((resolve,reject)=> {console.log('Something');resolve(3);})})
.then((res)=>{console.log(res);return new Promise((resolve,reject)=> {console.log('Something');resolve(4);})})
console.log("When?")
在输出中,首先打印的是“何时”。。现在我不明白这是怎么可能的,因为第一个then中的回调只会在大约10秒钟内运行,并且只有在这之后,回调才会返回一个承诺,将运行第二个then?
也许我不明白then的回调如何与“then”函数配合使用。
我认为然后实现看起来有点像:
then(callback){
let value= callback();
if(value is promise){
return value;
}
return Promise.resolve(value)
}
看起来你对Javascript还是新手。我也是,这给我带来了很多麻烦。console.log将始终首先打印,因为它不在“then”中。Js将异步运行日志,这意味着它不会等待承诺完成。为了解决这个问题或更好,为了确保不会发生这种情况,请使用.then中的日志或使用promiseName.all(promiseName).then(在此处登录) 希望我能帮上忙! 这可能也有帮助
PS:我的第一个答案,如果我做错了什么,请告诉我:好问题。答案是事件循环,它评估JavaScript。简言之,有微观任务和宏观任务。控制台日志是宏任务,承诺是微任务。事件循环首先处理宏任务,然后处理微任务 有关更深入的解释,请参见:
问题中有一部分代码几乎相同。搜索
console.log('start')代码>找到它。这是因为承诺中的代码是异步的,异步代码总是在所有同步代码执行完毕后执行
在您的示例中,有两条同步指令,第一行是设置承诺链和控制台.log
。因此,JavaScript首先设置承诺,然后在
时记录。只有在这之后,它才会一个接一个地解决承诺
如果您想使代码按预期的方式运行,您可以将其包装成一个异步函数
,然后使用wait
关键字停止同步代码的执行,直到解决了承诺:
//async is actually creating a promise under the hood for you
async function imAsyncFunction () {
// await will make sure that before proceeding to the next
// synchronous instruction (the console.log) the promise chain is resolved
await Promise.resolve('333')
.then((res)=>{console.log(res);return new Promise((resolve,reject)=> {setTimeout(function(){console.log('Timeout');resolve(1);},10000);console.log('Something');})})
.then((res)=>{console.log(res);return new Promise((resolve,reject)=> {console.log('Something');resolve(2);})})
.then((res)=>{console.log(res);return new Promise((resolve,reject)=> {console.log('Something');resolve(3);})})
.then((res)=>{console.log(res);return new Promise((resolve,reject)=> {console.log('Something');resolve(4);})});
console.log("When?");
}
imAsyncFunction();
// the log produced will be:
// 333
// Something
// Timeout
// 1
// Something
// 2
// Something
// 3
// Something
// When?
首先,谢谢你。。你太棒了。。但我不想解决这个问题,我想理解它。你发给我的链接,我尽可能熟悉、阅读并从中获取。。但是仍然不能理解then
的实现尽管我花了很多时间阅读和“玩”itUm,让我来帮助你。因此,您可以看到“then”它实际执行的操作,它等待前面的操作完成,然后在将其写入“.then”之后继续执行代码。我知道这有很多,但我不知道还有什么能帮助你理解。“then”之外的代码总是首先运行,因为它是异步的。上面的代码是否运行并不重要,因为它不是。这是另外一回事。谢谢你的回答。但我很难理解“return-Promise.resolve(value)”这一行是如何运行的。。也许我需要找到一种更清晰的方式来提问。同时,我对你的答案投了赞成票,因为我从iti中学到了一些东西。我对你的答案投了赞成票,但因为它不能百分之百地回答我的问题,所以我宁愿不批准它。谢谢!不客气!让我知道什么还不清楚,我可以进一步说明